Pandas详解七之DatetimeIndex、PeriodIndex和TimedeltaIndex时间序列
2018-03-17 22:34
1021 查看
约定:
代码结果:
通过.to_period()将DatetimeIndex序列转换为PeriodIndex序列:
代码结果:
将DetetimeIndex序列转换为TimedeltaIndex序列:
代码结果:
三者提供了许多与时间有关的属性:
代码结果:
代码结果:
代码结果:
通过DatetimeIndex.shift()移动时间点:
代码结果:
通过DatetimeIndex.normalize()将时刻修改为当天的凌晨零点:
代码结果:
作为Series对象的索引:
代码结果:
通过between_time()返回位于指定时间段的数据集:
代码结果:
通过tshift()将索引移动指定的时间:
代码结果:
作为Seires的列:
代码结果:
通过属性dt调用时间属性:
代码结果:
谢谢大家的浏览,
希望我的努力能帮助到您,
共勉!
import pandas as pd import numpy as np
时间序列
上节介绍的Timestamp、Period和Timedelta对象都是单个值,这些值都可以放在索引或数据中。作为索引的时间序列有:DatetimeIndex、PeriodIndex和TimedeltaIndex,它们都可以作为Series和DataFrame的索引。一、创建DatetimeIndex、PeriodIndex和TimedeltaIndex时间序列
调用pd.date_range()创建DatetimeIndex序列:index=pd.date_range("2018-03-17","2018-03-30",freq="2H") loc=np.random.choice(np.arange(len(index)),size=4,replace=False)#随机选取4个互不相同的数 loc.sort() ts_index=index[loc] ts_index
代码结果:
DatetimeIndex(['2018-03-17 14:00:00', '2018-03-18 08:00:00', '2018-03-25 00:00:00', '2018-03-27 22:00:00'], dtype='datetime64[ns]', freq=None)
通过.to_period()将DatetimeIndex序列转换为PeriodIndex序列:
pd_index=ts_index.to_period("D") pd_index
代码结果:
PeriodIndex(['2018-03-17', '2018-03-18', '2018-03-25', '2018-03-27'], dtype='period[D]', freq='D')
将DetetimeIndex序列转换为TimedeltaIndex序列:
longseconds=np.diff(ts_index) td_index=pd.TimedeltaIndex(longseconds) td_index
代码结果:
TimedeltaIndex(['0 days 18:00:00', '6 days 16:00:00', '2 days 22:00:00'], dtype='timedelta64[ns]', freq=None)
三者提供了许多与时间有关的属性:
ts_index.weekday
代码结果:
Int64Index([5, 6, 6, 1], dtype='int64')
pd_index.month
代码结果:
Int64Index([3, 3, 3, 3], dtype='int64')
td_index.seconds
代码结果:
Int64Index([64800, 57600, 79200], dtype='int64')
通过DatetimeIndex.shift()移动时间点:
ts_index.shift(2,"2H")
代码结果:
DatetimeIndex(['2018-03-17 18:00:00', '2018-03-18 12:00:00', '2018-03-25 04:00:00', '2018-03-28 02:00:00'], dtype='datetime64[ns]', freq=None)
通过DatetimeIndex.normalize()将时刻修改为当天的凌晨零点:
ts_index.normalize()
代码结果:
DatetimeIndex(['2018-03-17', '2018-03-18', '2018-03-25', '2018-03-27'], dtype='datetime64[ns]', freq=None)
二、将时间序列作为索引
TimestampIndex,PeriodIndex和TimedeltaIndex都可以作为Series、Dataframe对象的索引和列,在此只介绍一种。作为Series对象的索引:
ts_series=pd.Series(range(4),index=ts_index) ts_series
代码结果:
2018-03-17 14:00:00 0 2018-03-18 08:00:00 1 2018-03-25 00:00:00 2 2018-03-27 22:00:00 3 dtype: int32
通过between_time()返回位于指定时间段的数据集:
ts_series.between_time("7:00","17:00")
代码结果:
2018-03-17 14:00:00 0 2018-03-18 08:00:00 1 dtype: int32
通过tshift()将索引移动指定的时间:
ts_series.tshift(1,"2D")
代码结果:
2018-03-19 14:00:00 0 2018-03-20 08:00:00 1 2018-03-27 00:00:00 2 2018-03-29 22:00:00 3 dtype: int32
作为Seires的列:
ts_data=pd.Series(ts_index) ts_data
代码结果:
0 2018-03-17 14:00:00 1 2018-03-18 08:00:00 2 2018-03-25 00:00:00 3 2018-03-27 22:00:00 dtype: datetime64[ns]
通过属性dt调用时间属性:
ts_data.dt.hour
代码结果:
0 14 1 8 2 0 3 22 dtype: int64
谢谢大家的浏览,
希望我的努力能帮助到您,
共勉!
相关文章推荐
- Pandas详解六之Timestamp、Period、Timedelta时间对象
- python时间日期函数与利用pandas进行时间序列处理详解
- pandas 将“字符类型的日期列”转化成“时间戳索引(DatetimeIndex)”
- Python's datetime module, numpy's datetime64/timedelta64 and pandas' Timestamp/Timedelta objects
- C# DateTime ToString 时间转换详解
- 微信小程序 时间格式化(util.formatTime(new Date))详解
- Delphi 判断时间是否合法 -IsValidDateTime、IsValidDate、IsValidTime、IsValidDateDay
- python time模块和datetime模块详解
- Pandas index详解
- c++ 时间类型详解 time_t
- pandas时间序列操作
- Pandas Timedelta对象
- python3 datetime和time获取当前日期和时间
- Python时间与日期操作(datetime、time、calendar)
- DecodeDate、DecodeTime ... DecodeDateTime ... 分解时间
- 【python】详解time模块功能asctime、localtime、mktime、sleep、strptime、strftime、time等函数以及时间的加减运算
- Java的日期与时间(十六)java.time.format.DateTimeFormatter
- 1.15 python/pandas时间序列和ARIMA(1)
- jquery easyui validatebox增加对time、date、datetime的验证,时间格式化
- 推荐一个时间操作的类——Time Period Library