python - 使用 datetime.date 对象列表过滤数据帧行

标签 python pandas

问题:

我正在尝试使用 datetime.date 对象列表来过滤我的数据框行。我得到 ValueError: Arrays were different lengths: 175033 vs 33

数据:

type(third_friday_lst)
list

type(third_friday_lst[0])
datetime.date

third_friday_lst

[datetime.date(2013, 1, 18),
 datetime.date(2013, 2, 15),
 datetime.date(2013, 3, 15),
 datetime.date(2013, 4, 19),
 datetime.date(2013, 5, 17),
 datetime.date(2013, 6, 21),
 datetime.date(2013, 7, 19),
 datetime.date(2013, 8, 16),
 datetime.date(2013, 9, 20),
 datetime.date(2013, 10, 18),
 datetime.date(2013, 11, 15)]

我尝试过的:

如果我使用以下代码,我将获得所需的输出,但仅当我使用 third_friday_lst[0] 指定列表中的第一项时:

data[data['dt-date'] == third_friday_lst[0]]

                       Date         Time    Open    High    Low   Last Volume   dt-date
Timestamp                               
2013-01-18 08:00:00 2013/1/18   08:00:00    7875.5  7876.0  7867.5  7870.5 1059 2013-01-18
2013-01-18 08:05:00 2013/1/18   08:05:00    7871.0  7878.5  7870.5  7877.5  511 2013-01-18
2013-01-18 08:10:00 2013/1/18   08:10:00    7877.0  7879.0  7875.5  7875.5  226 2013-01-18
2013-01-18 08:15:00 2013/1/18   08:15:00    7875.5  7878.0  7874.5  7876.0  162 2013-01-18

期望的输出:

我想使用 third_friday_lst 过滤 data 中的行,而不使用列表索引[0],因为我想要包含在列表。我宁愿不使用 ['dt-date'] 列,但如果需要这样做也没关系。

谢谢

最佳答案

你可以试试:

data[data['dt-date'].isin(third_friday_lst)]

谢谢!

关于python - 使用 datetime.date 对象列表过滤数据帧行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41404277/

相关文章:

python - 如何在 Django Rest Framework 响应中返回原始数据类型(而不是 UUID)

python - Raspberry Pi 2 上的 PyGame : Faster loading than pygame. image.load() 可能吗?

python - 从 bash 运行 python 时输入丢失字符串格式

python - 使用相同的字符串过滤前 3 行并在 python 中计算平均值

python - 如何进行多个查询?

python - 从API网关获取查询字符串参数

python - 将数据分箱到相同大小的箱中

python - pandas groupby 有很多键

python - Pandas DataFrame - 根据键删除/替换字典值

python - 除了一只 Pandas ,所有行总和