Python pandas Date
Posted 卷积
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python pandas Date相关的知识,希望对你有一定的参考价值。
Pandas主要有4中与时间相关的类型。Timestamp, Period, DatetimeIndex,PeriodIndex.
import pandas as pd import numpy as np # #Timestamp pd.Timestamp(\'9/1/2016 10:05AM\') #output: Timestamp(\'2016-09-01 10:05:00\') # #Period pd.Period(\'1/2016\') #output: Period(\'2016-01\', \'M\') pd.Period(\'3/5/2016\') #output: Period(\'2016-03-05\', \'D\') # #DatetimeIndex t1 = pd.Series(list(\'abc\'), [pd.Timestamp(\'2016-09-01\'), pd.Timestamp(\'2016-09-02\'), pd.Timestamp(\'2016-09-03\')]) t1 """ 2016-09-01 a 2016-09-02 b 2016-09-03 c dtype: object """ type(t1.index) #pandas.tseries.index.DatetimeIndex # #PeriodIndex t2 = pd.Series(list(\'def\'), [pd.Period(\'2016-09\'), pd.Period(\'2016-10\'), pd.Period(\'2016-11\')]) t2 """ 2016-09 d 2016-10 e 2016-11 f Freq: M, dtype: object """ type(t2.index) # pandas.tseries.period.PeriodIndex
1. 关于时间类型的转换
#Converting-to-Datetime d1 = [\'2 June 2013\', \'Aug 29, 2014\', \'2015-06-26\', \'7/12/16\'] ts3 = pd.DataFrame(np.random.randint(10, 100, (4,2)), index=d1, columns=list(\'ab\')) ts3
ts3.index = pd.to_datetime(ts3.index)
ts3
pd.to_datetime(\'4.7.12\', dayfirst=True) #output: Timestamp(\'2012-07-04 00:00:00\')
2. 时间间隔
##Timedeltas pd.Timestamp(\'9/3/2016\')-pd.Timestamp(\'9/1/2016\') # Timedelta(\'2 days 00:00:00\') pd.Timestamp(\'9/2/2016 8:10AM\') + pd.Timedelta(\'12D 3H\') # Timestamp(\'2016-09-14 11:10:00\')
3. Dataframe中的时间
dates = pd.date_range(\'10-01-2016\', periods=9, freq=\'2W-SUN\') dates """ DatetimeIndex([\'2016-10-02\', \'2016-10-16\', \'2016-10-30\', \'2016-11-13\', \'2016-11-27\', \'2016-12-11\', \'2016-12-25\', \'2017-01-08\', \'2017-01-22\'], dtype=\'datetime64[ns]\', freq=\'2W-SUN\') """ df = pd.DataFrame({\'Count 1\': 100 + np.random.randint(-5, 10, 9).cumsum(), \'Count 2\': 120 + np.random.randint(-5, 10, 9)}, index=dates) df
df.index.weekday_name """ array([\'Sunday\', \'Sunday\', \'Sunday\', \'Sunday\', \'Sunday\', \'Sunday\', \'Sunday\', \'Sunday\', \'Sunday\'], dtype=object) """ df.diff()
df.resample(\'M\').mean()
df[\'2017\']
df[\'2016-12\']
df[\'2016-12\':]
以上是关于Python pandas Date的主要内容,如果未能解决你的问题,请参考以下文章