[英]How to index a pandas dataframe by datetime?
我正在使用 django_pandas package 从存储的 Django 模型中获取 Pandas dataframe。
df = qs.to_dataframe(['time', 'price_open', 'price_high', 'price_low', 'price_close'], index='time')
现在我想按日期时间访问 dataframe,但是,这不起作用。 打印 df 看起来像这样,其中时间列有一个奇怪的 position:
如果我执行print(df.keys())
我会得到以下结果: Index(['price_open', 'price_high', 'price_low', 'price_close', ]], dtype='object') 但我更期待时间。 此外, df.columns 不包含时间,而只包含其他列。
如何在给定的日期时间访问 df? 为什么时间不是 df 的关键? 谢谢!
正如@ThePorcius 所指出的, reset_index
应该会返回时间列。
df = df.reset_index()
根据文档,您可以在resample
中使用on
参数来使用列而不是索引。 您需要确保time
列是datetime
。
dailyFrame=(
df.resample('D', on='time')
.agg({'price_open': 'first', 'price_high': 'max', 'price_low': 'min', 'price_close': 'last'})
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.