让我们考虑 3 个表:
每本书都有一个指向其作者的外键,可以在美国表中,也可以在英国表中。
如何在 SQLAlchemy 中实现这样的外键条件?
我想要一列来处理链接。
到目前为止,我的方法是创建一个抽象类Author,从它AmericanAuthor和BritishAuthor继承,并具有Book指向父级的外键。
class Author(Model):
__abstract__ = True
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
class AmericanAuthor(Author):
__tablename__ = 'american_author'
# some other stuff
class BritishAuthor(Author):
__tablename__ = 'british_author'
# some other stuff
class Book(Model):
__tablename__ = 'book'
title = db.Column(db.String)
author_id = db.Column(db.Integer, db.ForeignKey("author.id"))
Run Code Online (Sandbox Code Playgroud)
它失败并出现错误:
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'books.author_id' could not find table 'author' with which to generate a …Run Code Online (Sandbox Code Playgroud) 我有以下架构:
type Post {
id: ID!
text: String
}
Run Code Online (Sandbox Code Playgroud)
我正在使用自动生成的突变neo4j-graphql.js,因此我可以访问以下突变:
UpdatePost(
id: ID!
text: String
): Post
Run Code Online (Sandbox Code Playgroud)
问题:
当我使用以下查询时:
mutation update($id: String, $text: String) {
UpdatePost(id: $id, text: $text) {
id
text
}
}
Run Code Online (Sandbox Code Playgroud)
具有以下参数:
{
"id": "a19289b3-a191-46e2-9912-5a3d1b067cb2",
"text": "text"
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
{
"error": {
"errors": [
{
"message": "Variable \"$id\" of type \"String\" used in position expecting type \"ID!\".",
"locations": [
{
"line": 1,
"column": 17
},
{
"line": 2,
"column": 18
}
],
"extensions": {
"code": …Run Code Online (Sandbox Code Playgroud) 我有以下数据框:
event_id occurred_at user_id
19148 2015-10-01 1
19693 2015-10-05 2
20589 2015-10-12 1
20996 2015-10-15 1
20998 2015-10-15 1
23301 2015-10-23 2
23630 2015-10-26 1
25172 2015-11-03 1
31699 2015-12-11 1
32186 2015-12-14 2
43426 2016-01-13 1
68300 2016-04-04 2
71926 2016-04-19 1
Run Code Online (Sandbox Code Playgroud)
我想按时间顺序(1 到 n)对每个用户的事件进行排名。
我可以通过这样做来实现这一点:
df.groupby('user_id')['occurred_at'].rank(method='dense')
Run Code Online (Sandbox Code Playgroud)
但是,对于同一日期(同一用户)发生的这两行,我最终得到相同的排名:
20996 2015-10-15 1
20998 2015-10-15 1
Run Code Online (Sandbox Code Playgroud)
如果事件日期相同,我想比较event_id并任意排名最低的事件event_id。我怎样才能轻松实现这一目标?
我可以对排名进行后期处理,以确保每个排名仅使用一次,但这看起来相当庞大......
编辑:如何重现:
将数据复制粘贴到data.csv文件中。然后 :
import pandas as pd
df = pd.read_csv('data.csv', delim_whitespace=True)
df['rank'] = df.groupby('user_id')['occurred_at'].rank(method='dense')
>>> df[df['user_id'] …Run Code Online (Sandbox Code Playgroud) 我有一个包含一些基本 docker 命令的 Makefile:
all: build
build:
docker build .
DOCKER_CONTAINERS=$(docker ps -a -q)
DOCKER_IMAGES=$(docker images -q)
clean:
ifdef $(DOCKER_CONTAINERS)
@echo "Stopping Docker containers..."
docker stop $(DOCKER_CONTAINERS)
@echo "Deleting Docker containers..."
docker rm $(DOCKER_CONTAINERS)
endif
iclean:
ifdef $(DOCKER_IMAGES)
@echo "Deleting Docker images..."
docker rmi $(DOCKER_IMAGES)
endif
Run Code Online (Sandbox Code Playgroud)
并想使用最后一张来清理现有图像,我确实有一些图像:
$ docker images -q
aaa5a74f6006
099dec0df83b
c9889c81a971
bddca31db222
d85328b9bf58
dd6f76d9cc90
Run Code Online (Sandbox Code Playgroud)
然而,DOCKER_IMAGES规则中似乎没有定义(我稍后通过显示它来仔细检查):
$ make iclean
make: Nothing to be done for 'iclean'.
Run Code Online (Sandbox Code Playgroud)
它适用于clean:
$ make clean
Stopping Docker containers...
docker …Run Code Online (Sandbox Code Playgroud) 我有一个数据框 myDF,我希望使用其他列的条件组合将其中的一列设置为零,并使用第二个数据框 CriteriaDF 进行索引。
myDF.head():
DateTime GrossPowerMW USDateTime_string DateTime_timestamp \
0 01/01/1998 00:00 17.804 01/01/1998 00:00 1998-01-01 00:00:00
1 01/01/1998 01:00 18.751 01/01/1998 01:00 1998-01-01 01:00:00
2 01/01/1998 02:00 20.501 01/01/1998 02:00 1998-01-01 02:00:00
3 01/01/1998 03:00 22.222 01/01/1998 03:00 1998-01-01 03:00:00
4 01/01/1998 04:00 24.437 01/01/1998 04:00 1998-01-01 04:00:00
Month Day Hour GrossPowerMW_Shutdown
0 1 3 0 17.804
1 1 3 1 18.751
2 1 3 2 20.501
3 1 3 3 22.222
4 1 3 4 24.437
Run Code Online (Sandbox Code Playgroud)
标准DF: …
我已经对熊猫数据框进行了切片。
end_date = df[-1:]['end']
type(end_date)
Out[4]: pandas.core.series.Series
end_date
Out[3]:
48173 2017-09-20 04:47:59
Name: end, dtype: datetime64[ns]
Run Code Online (Sandbox Code Playgroud)
48173并只获取2017-09-20 04:47:59字符串?我必须使用2017-09-20 04:47:59作为参数来调用 REST API ,所以我必须从 Pandasdatetime64系列中获取字符串。48173并只获取 datetime 对象 [类似的东西datetime.datetime.strptime('2017-09-20 04:47:59', '%Y-%m-%d %H:%M:%S')]。我需要它,因为稍后我将不得不检查是否'2017-09-20 04:47:59' < datetime.datetime(2017,1,9) 我只需要转换一个单元格值,而不是整列。如何进行这些转换?
python ×4
pandas ×3
apollo ×1
datetime64 ×1
docker ×1
grandstack ×1
graphql ×1
indexing ×1
loops ×1
makefile ×1
postgresql ×1
python-3.x ×1
sqlalchemy ×1