Pandas:我如何在我的时间序列中的每一个新的一天重新启动扩展功能?

vkc1a9a2  于 6个月前  发布在  其他
关注(0)|答案(1)|浏览(47)

我在我的数据集中使用扩展方法。我知道如何使用它,例如:

data["someColumn"].expanding().mean()

字符串
挑战是我的数据集包含时间序列,当新的一天开始时,我需要“重新启动”扩展方法。即,当新的一天开始扩展时,应该将新的一天的第一行视为唯一可用的数据,然后第二行是第二个数据,直到一天结束。
我怎么才能做到呢?

ymdaylpp

ymdaylpp1#

假设你的时间序列被用作你的索引,你可以用索引日期创建一个新的列,然后执行df.groupby("date").expanding().mean()

import pandas as pd

df = pd.DataFrame(
    {"B": [1, 2, 4, 0, 4]},
    index=pd.to_datetime(
        ["2023-12-11 21:00:00", "2023-12-11 22:00:00", "2023-12-11 23:00:00",
         "2023-12-12 00:00:00", "2023-12-12 01:00:00"]
    )
)

# Uncomment this line if you want the index to be sorted.
# df = df.sort_index()

df["day"] = df.index.to_series().dt.strftime("%Y-%m-%d")

df.groupby("day").expanding().mean()
# Returns:
#
#                                        B
# day                                     
# 2023-12-11 2023-12-11 21:00:00  1.000000
#            2023-12-11 22:00:00  1.500000
#            2023-12-11 23:00:00  2.333333
# 2023-12-12 2023-12-12 00:00:00  0.000000
#            2023-12-12 01:00:00  2.000000

字符串

相关问题