数据蛙数据分析每周作业

Pandas在时间日期处理的应用

2019-02-06  本文已影响3人  圆圆KK

Pandas在时间日期上的应用十分方便,能够快速处理、生成数据,这里总结其部分应用。
常用类型:

常用方法
Timestamp(时间戳) to_datetime / date_range
Timedelta(时间间隔) to_timedelta / timedelta_range
Period(时间段) Period / period_range
DateOffset(时间偏移) DateOffset
1. Timestamp

Timestamp是从标准库的datetime类继承过来,可以参考datetime的应用。

>>> pd.Timestamp.now() #生成当前时间
Timestamp('2019-01-31 19:58:18.282574')
>>> pd.Timestamp(2017, 1, 1, 12)
Timestamp('2017-01-01 12:00:00')
>>> a = pd.Timestamp("2019-02-01")
>>> a.day
1

to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False, utc=None, box=True, format=None, exact=True, unit=None, infer_datetime_format=False, origin='unix', cache=False)



>>> pd.to_datetime("11/10/12",format="%y/%m/%d")
Timestamp('2011-10-12 00:00:00')
>>> df = pd.DataFrame({'year': [2015, 2016],'month': [2, 3],'day': [4, 5]})
>>> pd.to_datetime(df)
0   2015-02-04
1   2016-03-05
dtype: datetime64[ns]

date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False, name=None, closed=None, **kwargs)



>>> pd.date_range("2001-01-01", periods=10, freq='2h20min')
DatetimeIndex(['2001-01-01 00:00:00', '2001-01-01 02:20:00',
               '2001-01-01 04:40:00', '2001-01-01 07:00:00',
               '2001-01-01 09:20:00', '2001-01-01 11:40:00',
               '2001-01-01 14:00:00', '2001-01-01 16:20:00',
               '2001-01-01 18:40:00', '2001-01-01 21:00:00'],
              dtype='datetime64[ns]', freq='140T')

date_range生成的为DatetimeIndex,可以转化为Series或者DataFrame

>>> pd.Series(pd.date_range("2001-01-01", periods=5, freq='2h20min'))
0   2001-01-01 00:00:00
1   2001-01-01 02:20:00
2   2001-01-01 04:40:00
3   2001-01-01 07:00:00
4   2001-01-01 09:20:00
dtype: datetime64[ns]

转化为DataFrame不设定列索引则默认为0

>>> pd.DataFrame({"time":pd.date_range("2001-01-01", periods=5, freq='2h20min')},index=list("abcde"))
time
a   2001-01-01 00:00:00
b   2001-01-01 02:20:00
c   2001-01-01 04:40:00
d   2001-01-01 07:00:00
e   2001-01-01 09:20:00

2.Timedelta

Timedelta相当于Python的datetime.timedelta

to_timedelta(arg, unit='ns', box=True, errors='raise')


>>> >>> pd.to_timedelta('1 days 06:05:01.00003')
Timedelta('1 days 06:05:01.000030')
#box的区别
>>> pd.to_timedelta([2,4,6,8,9], unit='d')
TimedeltaIndex(['2 days', '4 days', '6 days', '8 days', '9 days'], dtype='timedelta64[ns]', freq=None)
>>> pd.to_timedelta([2,4,6,8,9],unit= "D",box=False)
array([172800000000000, 345600000000000, 518400000000000, 691200000000000,
       777600000000000], dtype='timedelta64[ns]')

timedelta_range(start=None, end=None, periods=None, freq=None, name=None, closed=None)

>>> pd.timedelta_range(start='1 day', periods=4)
TimedeltaIndex(['1 days', '2 days', '3 days', '4 days'], dtype='timedelta64[ns]', freq='D')
3.Period

period_range(start=None, end=None, periods=None, freq=None, name=None)

ps: start, end, periods三个参数必须指定两个参数

>>> pd.period_range(start='2018-01-01', end='2019-01-01', freq='M')
PeriodIndex(['2018-01', '2018-02', '2018-03', '2018-04', '2018-05', '2018-06',
             '2018-07', '2018-08', '2018-09', '2018-10', '2018-11', '2018-12',
             '2019-01'],
            dtype='period[M]', freq='M')
4.DateOffset

DateOffset(n=1, normalize=False, **kwds)



>>> a = pd.Timestamp('2017-01-01 09:10:11')

>>> a + pd.DateOffset(month=3)
Timestamp('2017-03-01 09:10:11')

>>> a + pd.DateOffset(months=3)
Timestamp('2017-04-01 09:10:11')

接下来会分享怎样把Numpy和Pandas应用至具体案例中,关于Numpy的入门教程,有需要的会补充。

上一篇 下一篇

猜你喜欢

热点阅读