Pandas mainly has 4 of the time-related types. Timestamp, Period, Datetimeindex,periodindex.
ImportPandas as PDImportNumPy as NP##TimestampPd. Timestamp ('9/1/2016 10:05am')#output:timestamp (' 2016-09-01 10:05:00 ')##PeriodPd. Period ('1/2016')#output:period (' 2016-01 ', ' M ')Pd. Period ('3/5/2016')#output:period (' 2016-03-05 ', ' D ')##DatetimeindexT1 = PD. Series (List ('ABC'), [PD. Timestamp ('2016-09-01'), PD. Timestamp ('2016-09-02'), PD. Timestamp ('2016-09-03')]) T1"""2016-09-01 a2016-09-02 b2016-09-03 cdtype:object"""type (t1.index)#Pandas.tseries.index.DatetimeIndex##Periodindext2 = PD. Series (List ('def'), [PD. Period ('2016-09'), PD. Period ('2016-10'), PD. Period ('2016-11')]) T2"""2016-09 d2016-10 e2016-11 ffreq:m, Dtype:object"""type (t2.index)#Pandas.tseries.period.PeriodIndex
1. About conversion of time types
# converting-to-datetimed1 = ['2 June'. '2015-06-26'7/12/16'= PD. DataFrame (Np.random.randint (Ten, (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. Time interval
# #TimedeltasPD. 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. Time in the 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': + Np.random.randint (-5, 10, 9). Cumsum (),'Count 2': + Np.random.randint ( -5, 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[')
df['2016-12']
df['2016-12':]
Python Pandas Date