【问题标题】:Python Dataframe.resample() deletes time from datetimeindexPython Dataframe.resample() 从 datetimeindex 中删除时间
【发布时间】:2017-12-03 11:53:13
【问题描述】:

由于某种原因,重采样到 24H1D 会缩短 DateTimeIndex 的时间。

我正在使用
蟒蛇:3.5.3/康达
numpy:1.11.3
熊猫:0.20.2

df = pd.read_csv(inFile, parse_dates=True)

print(df.head())
df = df.resample("1D").agg({'open':'first','high':'max','low' :'min','close': 'last','volume': 'sum'}).dropna()

print(df.head()

第一个打印语句

第二

【问题讨论】:

  • 您的期望是什么?这对我来说似乎很正常。
  • 如果您检查索引,您会看到它有一个 freq 属性,当您重新采样时,它会从 None 变为 'D'。 2年前的这个答案中对此进行了某种解释,并且看起来无法更改(至少在2年前)stackoverflow.com/questions/27607974/…无论如何,请注意这是一个显示问题,索引类型有没有改变。
  • @ayhan - 我也想看看打印的时间 (00:00:00)
  • @JohnE:是的,索引类型是正确的。但是当我在上面运行 strptime 时,它​​会抱怨: ValueError: time data '2017-05-30' does not match format '%Y-%m-%d %H:%M:%S'
  • 但是最大值、最小值或最后值没有出现在 00:00?做一个聚合,你会从每小时到每天的数据。如果您只获得第一个值,但对于其他值,我会理解,我认为该索引是错误的。

标签: python pandas numpy dataframe time-series


【解决方案1】:

想通了

df.index = pd.to_datetime(df.index.format(formatter=lambda x: x.strftime('%Y-%m-%d %H:%M:%S'))) 

【讨论】:

    猜你喜欢
    • 2023-02-22
    • 2018-10-15
    • 2020-11-28
    • 2019-03-07
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多