【问题标题】:Create custom sized bins of datetime Series in Pandas在 Pandas 中创建自定义大小的日期时间系列容器
【发布时间】:2023-01-17 23:47:26
【问题描述】:

我有多个 Pandas 系列的 datetime64 值,我想使用任意 bin 大小将它们分组。

我找到了 Series.to_period() 函数,它完全符合我的要求,只是我需要更多地控制所选的 bin 大小。 to_period 允许我按整年、月、日等分类,但我也想按 5 年、6 小时或 15 分钟分类。使用像 5Y6H15min 这样的语法在 Pandas 的其他角落有效,但显然不在这里。

s = pd.Series(["2020-02-01", "2020-02-02", "2020-02-03", "2020-02-04"], dtype="datetime64[ns]")

# Output as expected
s.dt.to_period("M").value_counts()
2020-02    4
Freq: M, dtype: int64

# Output as expected
s.dt.to_period("W").value_counts()
2020-01-27/2020-02-02    2
2020-02-03/2020-02-09    2
Freq: W-SUN, dtype: int64

# Output as expected
s.dt.to_period("D").value_counts()
2020-02-01    1
2020-02-02    1
2020-02-03    1
2020-02-04    1
Freq: D, dtype: int64

# Output unexpected (and wrong?)
s.dt.to_period("2D").value_counts()
2020-02-01    1
2020-02-02    1
2020-02-03    1
2020-02-04    1
Freq: 2D, dtype: int64

【问题讨论】:

    标签: python pandas datetime binning


    【解决方案1】:

    我相信 pd.Grouper 就是您要找的。

    https://pandas.pydata.org/docs/reference/api/pandas.Grouper.html

    除了标准频率之外,它还提供了具有多个频率的灵活性:https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases

    从文档中:

    >>> start, end = '2000-10-01 23:30:00', '2000-10-02 00:30:00'
    >>> rng = pd.date_range(start, end, freq='7min')
    >>> ts = pd.Series(np.arange(len(rng)) * 3, index=rng)
    >>> ts
    2000-10-01 23:30:00     0
    2000-10-01 23:37:00     3
    2000-10-01 23:44:00     6
    2000-10-01 23:51:00     9
    2000-10-01 23:58:00    12
    2000-10-02 00:05:00    15
    2000-10-02 00:12:00    18
    2000-10-02 00:19:00    21
    2000-10-02 00:26:00    24
    Freq: 7T, dtype: int64
    
    >>> ts.groupby(pd.Grouper(freq='17min')).sum()
    2000-10-01 23:14:00     0
    2000-10-01 23:31:00     9
    2000-10-01 23:48:00    21
    2000-10-02 00:05:00    54
    2000-10-02 00:22:00    24
    Freq: 17T, dtype: int64
    

    笔记: 如果您想.groupby某个列,请使用以下语法: df.groupby(pd.Grouper(key="my_col", freq="3M"))

    【讨论】:

      猜你喜欢
      • 2018-09-16
      • 1970-01-01
      • 2021-03-25
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      • 2012-08-24
      • 2021-01-30
      相关资源
      最近更新 更多