【发布时间】:2019-08-15 14:02:06
【问题描述】:
我有一个 numpy 数组,我希望按日期时间过滤它。我目前具有将输入日期时间(start 和 end)与数据框进行比较的功能,如下所示:
if trim:
columns = input_hdf.columns.get_level_values(0)
print(str(columns))
print(start)
print(end)
if start is not None and end is not None:
mask = (columns >= start) & (columns <= end)
elif start is not None:
mask = (columns >= start)
elif end is not None:
mask = (columns <= end)
else:
# Should never reach this point, but just in case - mask will not affect the data
mask = True
input_hdf = input_hdf.loc[:, mask]
但是,我想为开始和结束添加功能以指定为“一年中的一天”,其中年份与比较无关 - 如果这一天晚于 10 月 1 日,则将其排除在外2001 年或 2021 年。
我目前正在通过以下方式将整数值转换为日期时间:
start = datetime.strptime(start, '%d-%m-%Y') if start else None
默认年份为 1900,这将成为比较的一部分。
【问题讨论】:
-
Numpy 数组通常包含跨所有行和列的单一类型(除非您的数组完全是日期时间),而 Pandas 数据帧包含跨列的不同类型。不清楚您最初使用的是什么数据。见How to make good reproducible pandas examples。
-
你说的是 Numpy 数组,但看起来你使用的是 pandas?我不是 pandas 专家,但它具有大量用于时间序列处理和日期比较的内置功能。
标签: python pandas numpy dataframe