【问题标题】:using dataframe to groupby() with datetimeindex使用 dataframe 到 groupby() 和 datetimeindex
【发布时间】:2020-02-15 04:38:22
【问题描述】:

我有一个带有日期时间索引的 DF(例如):

Date_Time            C_1
2018-09-13 9:20:00  69.45
2018-09-13 9:30:00  69.44
2018-09-13 9:40:00  69.33
2018-09-13 9:50:00  69.34
2018-09-13 10:00:00 69.36
2018-09-14 9:20:00  69.45
2018-09-14 9:30:00  69.44
2018-09-14 9:40:00  69.33
2018-09-14 9:50:00  69.34
2018-09-14 10:00:00 69.36

我正在尝试做一个groupby(),按天分组,执行以下操作:

#finding the days with in the group for filter
unique_days = pd.Series(np.unique(df.index.date))

#apply the groupby()
df.groupby(unique_days).groups

然而,这会返回一个空的 dic... 意味着某些东西不能正常工作。 unique_days 正在返回您所期望的。

0      2018-09-13
1      2018-09-14
2      2018-09-17
3      2018-09-18
4      2018-09-19
          ...    
265    2019-10-10
266    2019-10-11
267    2019-10-14
268    2019-10-15
269    2019-10-16
Length: 270, dtype: object

【问题讨论】:

    标签: python python-3.x pandas-groupby


    【解决方案1】:

    要解决这个问题,你需要做的就是:

    df.groupby(df.index.date)

    我显然试图使情况过于复杂。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 1970-01-01
      • 2019-01-31
      • 2021-03-09
      相关资源
      最近更新 更多