python resample函数_Python pandas.DataFrame.resample函数方法的使用
DataFrame.resample(rule, axis=0, closed=None, label=None, convention='start', kind=None, loffset=None, base=None, on=None, level=None, origin='start_day', offset=None)[source]
重新采样time-series数据。
频率转换和time series重采样的便捷方法。对象必须具有类似datetime的索引(DatetimeIndex, PeriodIndex或TimedeltaIndex),或将类似datetime的值传递给on或level关键字。
参数:rule : eDateOffset, Timedelta 或 str
表示目标转换的偏移字符串或对象。
axis:{0或‘index’, 1 或 ‘columns’}, 默认为 0
向上采样或向下采样使用哪一个轴。对于级数,默认值为0,
即沿着行。必须是DatetimeIndex, TimedeltaIndex
或PeriodIndex。
closed : {‘right’, ‘left’}, 默认为None
bin区间的哪一边是关闭的。
除了‘M’、‘A’、‘Q’、‘BM’、‘BA’、‘BQ’和‘W’之外,
所有频率偏移的默认值都是‘left’,它们的默认值都是‘right’。
label: {‘right’, ‘left’}, 默认为 None
用哪边标签来标记bucket。
除了‘M’、‘A’、‘Q’、‘BM’、‘BA’、‘BQ’和‘W’之外,
所有频率偏移的默认值都是‘left’,它们的默认值都是‘right’。
convention :{'start', 'end', 's', 'e'}, 默认为 'start'
仅对于PeriodIndex,控制是使用规则的开始还是结束。
kind: {‘timestamp’, ‘period’}, 可选, 默认为 None
传递'timestamp'将结果索引转换为DateTimeIndex,
或'period'将其转换为PeriodIndex。默认情况下,
保留输入表示形式。
loffset : timedelta, 默认为None
调整重新采样的时间标签。
自1.1.0版本以来已弃用 : 您应该将loffset添加到df中。
重新取样后的索引。见下文。
base : int, 默认为0
对于平均细分1天的频率,聚合间隔的“origin”。
例如,对于“5min”频率,基数可以从0到4。默认值为0。
自1.1.0版本以来已弃用:
您应该使用的新参数是'offset'或'origin'。
on : str, 可选
自1.1.0版本以来已弃用:您应该使用的新参数是'offset'或'origin'。
level : str 或 int, 可选
用于重采样的多索引、级别(名称或数字)。级别必须与日期时间类似。
origin : {‘epoch’, ‘start’, ‘start_day’},
Timestamp 或 str, 默认为 ‘start_day’
调整分组的时间戳。原始时区必须与索引的时区匹配。
如果不使用时间戳,也支持以下值:
1) “epoch”:起源是1970年01月01日
2) 'start ': origin是timeseries的第一个值
2) “start_day”:起源是timeseries午夜的第一天
新版本1.1.0。
offset : Timedelta 或 str, 默认为 None
加到原点的偏移时间。
新版本1.1.0。
返回值:Resampler object
Notes
有关 更多信息,请参见用户指南。
要了解有关偏移字符串的更多信息,请参见此链接。
例子
首先创建一个带有9个一分钟时间戳的系列>>> index = pd.date_range('1/1/2000', periods=9, freq='T')
>>> series = pd.Series(range(9), index=index)
>>> series
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
2000-01-01 00:05:00 5
2000-01-01 00:06:00 6
2000-01-01 00:07:00 7
2000-01-01 00:08:00 8
Freq: T, dtype: int64
将系列下采样到3分钟的bin中,然后将落入bin中的时间戳记值相加>>> series.resample('3T').sum()
2000-01-01 00:00:00 3
2000-01-01 00:03:00 12
2000-01-01 00:06:00 21
Freq: 3T, dtype: int64
如上将系列降采样到3分钟的容器中,但使用右边缘而不是左侧标记每个容器。请注意,用作标签的存储桶中的值不包含在其标记的存储桶中。例如,在原始系列中,存储桶包含值3,但是带有标签的重新采样存储桶中的总和 不包括3(如果是,则总和将为6,而不是3)。要包含此值,请关闭bin间隔的右侧,如下面的示例所示。2000-01-01 00:03:002000-01-01 00:03:00>>> series.resample('3T', label='right').sum()
2000-01-01 00:03:00 3
2000-01-01 00:06:00 12
2000-01-01 00:09:00 21
Freq: 3T, dtype: int64
如上所述,将系列降采样到3分钟的箱中,但关闭箱间隔的右侧:>>> series.resample('3T', label='right', closed='right').sum()
2000-01-01 00:00:00 0
2000-01-01 00:03:00 6
2000-01-01 00:06:00 15
2000-01-01 00:09:00 15
Freq: 3T, dtype: int64
将series上采样到30秒箱中>>> series.resample('30S').asfreq()[0:5] # Select first 5 rows
2000-01-01 00:00:00 0.0
2000-01-01 00:00:30 NaN
2000-01-01 00:01:00 1.0
2000-01-01 00:01:30 NaN
2000-01-01 00:02:00 2.0
Freq: 30S, dtype: float64
将series上采样到30秒仓中,然后NaN 使用pad方法填充值>>> series.resample('30S').pad()[0:5]
2000-01-01 00:00:00 0
2000-01-01 00:00:30 0
2000-01-01 00:01:00 1
2000-01-01 00:01:30 1
2000-01-01 00:02:00 2
Freq: 30S, dtype: int64
将series上采样到30秒仓中,然后NaN使用bfill方法填充值>>> series.resample('30S').bfill()[0:5]
2000-01-01 00:00:00 0
2000-01-01 00:00:30 1
2000-01-01 00:01:00 1
2000-01-01 00:01:30 2
2000-01-01 00:02:00 2
Freq: 30S, dtype: int64
通过传递自定义函数apply>>> def custom_resampler(array_like):
... return np.sum(array_like) + 5
...
>>> series.resample('3T').apply(custom_resampler)
2000-01-01 00:00:00 8
2000-01-01 00:03:00 17
2000-01-01 00:06:00 26
Freq: 3T, dtype: int64
对于具有PeriodIndex的系列,关键字约定可用于控制使用rule的开始还是结束。
使用'start'约定按季度重新采样。将值分配给该期间的第一季度。>>> s = pd.Series([1, 2], index=pd.period_range('2012-01-01',
... freq='A',
... periods=2))
>>> s
2012 1
2013 2
Freq: A-DEC, dtype: int64
>>> s.resample('Q', convention='start').asfreq()
2012Q1 1.0
2012Q2 NaN
2012Q3 NaN
2012Q4 NaN
2013Q1 2.0
2013Q2 NaN
2013Q3 NaN
2013Q4 NaN
Freq: Q-DEC, dtype: float64
使用'end'约定按月重新采样季度。将值分配给该期间的最后一个月>>> q = pd.Series([1, 2, 3, 4], index=pd.period_range('2018-01-01',
... freq='Q',
... periods=4))
>>> q
2018Q1 1
2018Q2 2
2018Q3 3
2018Q4 4
Freq: Q-DEC, dtype: int64
>>> q.resample('M', convention='end').asfreq()
2018-03 1.0
2018-04 NaN
2018-05 NaN
2018-06 2.0
2018-07 NaN
2018-08 NaN
2018-09 3.0
2018-10 NaN
2018-11 NaN
2018-12 4.0
Freq: M, dtype: float64
对于DataFrame对象,关键字on可以用于指定列而不是用于重新采样的索引>>> d = dict({'price': [10, 11, 9, 13, 14, 18, 17, 19],
... 'volume': [50, 60, 40, 100, 50, 100, 40, 50]})
>>> df = pd.DataFrame(d)
>>> df['week_starting'] = pd.date_range('01/01/2018',
... periods=8,
... freq='W')
>>> df
price volume week_starting
0 10 50 2018-01-07
1 11 60 2018-01-14
2 9 40 2018-01-21
3 13 100 2018-01-28
4 14 50 2018-02-04
5 18 100 2018-02-11
6 17 40 2018-02-18
7 19 50 2018-02-25
>>> df.resample('M', on='week_starting').mean()
price volume
week_starting
2018-01-31 10.75 62.5
2018-02-28 17.00 60.0
对于具有MultiIndex的DataFrame,关键字级别可用于指定需要在哪个级别进行重采样>>> days = pd.date_range('1/1/2000', periods=4, freq='D')
>>> d2 = dict({'price': [10, 11, 9, 13, 14, 18, 17, 19],
... 'volume': [50, 60, 40, 100, 50, 100, 40, 50]})
>>> df2 = pd.DataFrame(d2,
... index=pd.MultiIndex.from_product([days,
... ['morning',
... 'afternoon']]
... ))
>>> df2
price volume
2000-01-01 morning 10 50
afternoon 11 60
2000-01-02 morning 9 40
afternoon 13 100
2000-01-03 morning 14 50
afternoon 18 100
2000-01-04 morning 17 40
afternoon 19 50
>>> df2.resample('D', level=0).sum()
price volume
2000-01-01 21 110
2000-01-02 22 140
2000-01-03 32 150
2000-01-04 36 90
如果要基于固定的时间戳调整垃圾箱的开始,请执行以下操作:>>> 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.resample('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
>>> ts.resample('17min', origin='epoch').sum()
2000-10-01 23:18:00 0
2000-10-01 23:35:00 18
2000-10-01 23:52:00 27
2000-10-02 00:09:00 39
2000-10-02 00:26:00 24
Freq: 17T, dtype: int64
>>> ts.resample('17min', origin='2000-01-01').sum()
2000-10-01 23:24:00 3
2000-10-01 23:41:00 15
2000-10-01 23:58:00 45
2000-10-02 00:15:00 45
Freq: 17T, dtype: int64
如果要使用偏移量 Timedelta 调整垃圾箱的开始,则以下两行等效:>>> ts.resample('17min', origin='start').sum()
2000-10-01 23:30:00 9
2000-10-01 23:47:00 21
2000-10-02 00:04:00 54
2000-10-02 00:21:00 24
Freq: 17T, dtype: int64
>>> ts.resample('17min', offset='23h30min').sum()
2000-10-01 23:30:00 9
2000-10-01 23:47:00 21
2000-10-02 00:04:00 54
2000-10-02 00:21:00 24
Freq: 17T, dtype: int64
要替换不推荐使用的base参数,现在可以使用offset,在本示例中,它等效于具有base = 2:>>> ts.resample('17min', offset='2min').sum()
2000-10-01 23:16:00 0
2000-10-01 23:33:00 9
2000-10-01 23:50:00 36
2000-10-02 00:07:00 39
2000-10-02 00:24:00 24
Freq: 17T, dtype: int64
要替换不推荐使用的loffset参数,请执行以下操作:>>> from pandas.tseries.frequencies import to_offset
>>> loffset = '19min'
>>> ts_out = ts.resample('17min').sum()
>>> ts_out.index = ts_out.index + to_offset(loffset)
>>> ts_out
2000-10-01 23:33:00 0
2000-10-01 23:50:00 9
2000-10-02 00:07:00 21
2000-10-02 00:24:00 54
2000-10-02 00:41:00 24
Freq: 17T, dtype: int64
python resample函数_Python pandas.DataFrame.resample函数方法的使用相关推荐
- python dataframe loc函数_python pandas.DataFrame.loc函数使用详解
官方函数 DataFrame.loc Access a group of rows and columns by label(s) or a boolean array. .loc[] is prim ...
- python数据去重的函数_python pandas dataframe 去重函数的具体使用
今天笔者想对pandas中的行进行去重操作,找了好久,才找到相关的函数 先看一个小例子 from pandas import Series, DataFrame data = DataFrame({' ...
- python convert函数_Python pandas.DataFrame.tz_convert函数方法的使用
DataFrame.tz_convert(tz, axis=0, level=None, copy=True)[source] 将tz-aware axis转换为目标时区. 参数:tz:str或 tz ...
- python iloc函数_Python pandas.DataFrame.iloc函数方法的使用
DataFrame.iloc 纯粹基于整数位置的索引,用于按位置选择. .iloc[] 主要是基于整数位置(从轴的0到长度-1),但也可以与布尔数组一起使用. 允许的输入:整数, 例如, 5 整数的列 ...
- python agg函数_Python pandas.DataFrame.agg函数方法的使用
DataFrame.agg(func, axis=0, *args, **kwargs) 使用指定axis上的一个或多个操作Aggregate. 参数:func: function, str, lis ...
- python describe函数_Python pandas.DataFrame.describe函数方法的使用
DataFrame.describe(self, percentiles=None, include=None, exclude=None) 生成描述性统计数据,总结数据集分布的集中趋势,分散和形状, ...
- python mul函数_Python pandas.DataFrame.mul函数方法的使用
DataFrame.mul(self, other, axis='columns', level=None, fill_value=None)DataFrame.multiply(self, othe ...
- python replace函数_Python pandas.DataFrame.replace函数方法的使用
DataFrame.replace(self, to_replace=None, value=None, inplace=False, limit=None, regex=False, method= ...
- python中cumsum函数_Python pandas.DataFrame.cumsum函数方法的使用
DataFrame.cumsum(self, axis=None, skipna=True, *args, **kwargs) 返回DataFrame或Series轴上的累计和. 返回包含累计和的相同 ...
最新文章
- Java EE---使用Spring框架创建Department小项目
- maven实战总结,工作中常见操作
- 栈(stack)和堆(heap)
- C语言fseek()函数(whence)重新定位文件指针位置
- JDBC基础教程:tutorialspoint-jdbc
- 关于Authorware的十二种使用技巧
- boost::make_recursive_variant相关的测试程序
- android daemon 程序,(转)Android App Daemon
- Android 设备启动时,APP应用自启动
- 装B指南之使用浏览器播放电影
- word 职称计算机考试大纲,全国职称计算机考试Word2003大纲.doc
- 我是不是得工作恐惧症了
- 几种表面缺陷检测数据集
- y480 linux无线网卡驱动,联想y480无线网卡驱动下载
- 《深入浅出WPF》——资源学习
- 3dmax中slice plane切割平面的作用
- 【托业】【新托业TOEIC新题型真题】学习笔记10-题库七-P7
- PC解决电子签名的方法
- 小米刷入Recovery
- SoX使用手册(中文版)
热门文章
- System.Data.SqlClient.SqlException: 用户 'IIS APPPOOL\y3' 登录失败
- 投基取巧:如何选择基金及构建投资组合?基金投资进阶经验分享,附思维导图!
- 如何利用免费小说引流?小说平台怎么免费推广引流?
- java eden space_《深入理解Java虚拟机》(六)堆内存使用分析,垃圾收集器 GC 日志解读...
- Android网络电话软件Sipdroid试用
- 制定当前学习目标与学习方法
- 【教程】10秒关闭手机QQ空间动态中的黄钻通知!再见了@黄钻官方团队!(以及关闭更多烦人的通知提醒)----2020.09.12
- 统计学习:现代机器学习
- 用 JS空格分隔手机号码 334格式
- JPG合成PDF在线网站免费合并