Date TIme in Python for data scientists

python date datatype- datetime
pandas date datatype- Timestamp

1) creating pandas datframe with string dates value

## datetime
date_data= pd.DataFrame(np.random.randint(10,100, (4,3)), columns=['A','B','C'])
dates= ['2 june 2013', '5 Aug 2015', '2015-07-09', '7/12/2014']
date_data.index= dates


A B C
2 june 2013 52 61 89
5 Aug 2015 21 69 89
2015-07-09 88 23 13
7/12/2014 43 39 21
creating string into pandas datetime format-

date_data.index= pd.to_datetime(date_data.index)
date_data

A B C
2013-06-02 59 93 77
2015-08-05 33 15 28
2015-07-09 63 19 25
2014-07-12 29 36 92
2) Time difference in pandas-

pd.Timestamp('12.03.2019')- pd.Timestamp('12 Aug 2018')

Timedelta('478 days 00:00:00')

3) Return a fixed frequency DatetimeIndex -


cum_array= pd.DataFrame({'colA': np.random.randint(1,5,9).cumsum(), 'colB': np.random.randint(-4,10,9)}) dates=pd.date_range('10-10-2019','15-10-2019', periods=9) cum_array.index= dates cum_array.diff() # provides difference

4) plotting time series

import matplotlib.pyplot as plt
%matplotlib inline
cum_array.plot()

<matplotlib.axes._subplots.AxesSubplot at 0x2fd605b8be0>
In [ ]:

Comments

Popular posts from this blog

Automatically open and do some actions on web pages in python using different packages

Explainability in Data Science:- Data, Model & Prediction