Time Series:
Pandas provide a robust tool for working time with Time series data, especially in the financial sector. While working with time series data, we frequently come across the following −
-
Generating sequence of time
-
Convert the time series to different frequencies
Pandas provides a relatively compact and self-contained set of tools for performing the above tasks.
Get Current Time:
datetime.now()
gives you the current date and time.
import
pandas as
pd
print
pd.
datetime.
now()
Its output
is as follows −
2017-05-11 06:10:13.393147
Create a TimeStamp:
Time-stamped data is the most basic type of timeseries data that associates values with points in time. For pandas objects, it means using the points in time. Let’s take an example –
import
pandas as
pd
print
pd.
Timestamp
(
'2017-03-01'
)
Its output
is as follows −
2017-03-01 00:00:00
It is also possible to convert integer or float epoch times. The
default unit for these is nanoseconds (since these are how Timestamps are stored). However, often epochs are stored in another unit which can be specified. Let’s take another example
import
pandas as
pd
print
pd.
Timestamp
(
1587687255
,
unit=
's'
)
Its output
is as follows −
2020-04-24 00:14:15
Create a Range of Time:
import
pandas as
pd
print
pd.
date_range(
"11:00"
,
"13:30"
,
freq=
"30min"
).
time
Its output
is as follows −
[datetime.time(11, 0) datetime.time(11, 30) datetime.time(12, 0)
datetime.time(12, 30) datetime.time(13, 0) datetime.time(13, 30)]
Change the Frequency of Time:
import
pandas as
pd
print
pd.
date_range(
"11:00"
,
"13:30"
,
freq=
"H"
).
time
Its output
is as follows −
[datetime.time(11, 0) datetime.time(12, 0) datetime.time(13, 0)]
Converting to Timestamps:
To convert a Series or list-like object of date-like objects, for
example strings, epochs, or a mixture, you can use the to_datetime
function. When passed, this returns a Series (with the same index), while a list-like
is converted to a DatetimeIndex
. Take a look at the following example −
import
pandas as
pd
print
pd.
to_datetime(
pd.
Series
([
'Jul 31, 2009'
,
'2010-01-10'
,
None
]))
Its output
is as follows −
0 2009-07-31
1 2010-01-10
2 NaT
dtype: datetime64[ns]
NaT
means Not a Time
(equivalent to NaN)
Let’s take another example.
import
pandas as
pd
print
pd.
to_datetime([
'2005/11/23'
,
'2010.12.31'
,
None
])
Its output
is as follows −
DatetimeIndex(['2005-11-23', '2010-12-31', 'NaT'], dtype='datetime64[ns]', freq=None)