首先,让我们创建一个每分钟的数据样本:
>>> index = pd.date_range('1/1/2000', periods=60*24*400, freq='T')
>>> series = pd.Series(range(60*24*400), index=index)
>>> series.head()
2000-01-01 00:00:00 0
2000-01-01 00:01:00 1
2000-01-01 00:02:00 2
2000-01-01 00:03:00 3
2000-01-01 00:04:00 4
当重采样到日常频率时,我可以看到closed
的影响:
>>> series.resample('D', closed="right").head()
1999-12-31 0.0 # <-- here, I get some data labeled 1999
2000-01-01 720.5
2000-01-02 2160.5
2000-01-03 3600.5
2000-01-04 5040.5
Freq: D, dtype: float64
>>> series.resample('D', closed="left").head()
2000-01-01 719.5
2000-01-02 2159.5
2000-01-03 3599.5
2000-01-04 5039.5
2000-01-05 6479.5
这就是我所期望的医生们的行为
关闭:{“右”,“左”} 中间间隔的哪一边是封闭的。
现在,如果我重述到一个月的频率:
>>> series.resample('M', closed="right").head()
2000-01-31 22319.5 # <-- ?
2000-02-29 65519.5
2000-03-31 108719.5
2000-04-30 152639.5
2000-05-31 196559.5
Freq: M, dtype: float64
>>> series.resample('M', closed="left").head()
2000-01-31 21599.5
2000-02-29 64079.5
2000-03-31 107279.5
2000-04-30 151199.5
2000-05-31 195119.5
Freq: M, dtype: float64
为什么我在使用1999-12-31
时没有得到closed="right"
示例
>>> pd.__version__
'0.22.0'
发布于 2018-03-16 11:16:03
这必须归功于label属性。您所链接的文档描述为
标签:{“右”,“左”}要用哪个边标签标记桶。除了“M”、“A”、“Q”、“BM”、“BA”、“BQ”和“W”之外,所有频率偏移的默认值都是“左”,它们的默认值都是“右”。
试着做:
series.resample('D', closed="right", label="right").head()
对于月份和日,您会看到不同的行为,因为默认的标签是“左”表示D,“右”表示M。
https://stackoverflow.com/questions/49317923
复制相似问题