Time series:

In [2]:
import numpy as np
import pandas as pd
In [7]:
rng = pd.date_range('1/1/2019', periods=100, freq='S')
In [10]:
ts = pd.Series(np.random.randint(0, 600, len(rng)), index=rng)
In [11]:
ts.resample('5Min').sum()
Out[11]:
2019-01-01    30551
Freq: 5T, dtype: int32

Time zone representation:

In [12]:
rng = pd.date_range('4/6/2019 00:00', periods=5, freq='D')
In [13]:
ts = pd.Series(np.random.randn(len(rng)), rng)
In [14]:
ts
Out[14]:
2019-04-06   -0.604794
2019-04-07    0.071687
2019-04-08    0.030639
2019-04-09   -0.464885
2019-04-10    0.281685
Freq: D, dtype: float64
In [15]:
ts_utc = ts.tz_localize('UTC')
In [16]:
ts_utc
Out[16]:
2019-04-06 00:00:00+00:00   -0.604794
2019-04-07 00:00:00+00:00    0.071687
2019-04-08 00:00:00+00:00    0.030639
2019-04-09 00:00:00+00:00   -0.464885
2019-04-10 00:00:00+00:00    0.281685
Freq: D, dtype: float64

Converting to another time zone:

In [21]:
ts_utc.tz_convert('US/Eastern')
Out[21]:
2019-04-05 20:00:00-04:00   -0.604794
2019-04-06 20:00:00-04:00    0.071687
2019-04-07 20:00:00-04:00    0.030639
2019-04-08 20:00:00-04:00   -0.464885
2019-04-09 20:00:00-04:00    0.281685
Freq: D, dtype: float64

Converting between time span representations:

In [22]:
rng = pd.date_range('1/1/2019', periods=6, freq='M')
In [23]:
ts = pd.Series(np.random.randn(len(rng)), index=rng)
In [24]:
ts
Out[24]:
2019-01-31    0.246631
2019-02-28   -0.209683
2019-03-31    0.118332
2019-04-30    0.273323
2019-05-31    0.496192
2019-06-30   -0.692572
Freq: M, dtype: float64
In [25]:
ps = ts.to_period()
In [26]:
ps
Out[26]:
2019-01    0.246631
2019-02   -0.209683
2019-03    0.118332
2019-04    0.273323
2019-05    0.496192
2019-06   -0.692572
Freq: M, dtype: float64
In [27]:
ps.to_timestamp()
Out[27]:
2019-01-01    0.246631
2019-02-01   -0.209683
2019-03-01    0.118332
2019-04-01    0.273323
2019-05-01    0.496192
2019-06-01   -0.692572
Freq: MS, dtype: float64

Converting between period and timestamp:
In the following example, we convert a quarterly frequency with year ending in June to 10am of
the end of the month following the quarter end:

In [3]:
prng = pd.period_range('2018', '2019Q4', freq='Q-JUN')
In [33]:
ts = pd.Series(np.random.randn(len(prng)), prng)
In [34]:
ts.index = (prng.asfreq('M', 'e') + 1).asfreq('H', 's') + 9
In [35]:
ts.head()
Out[35]:
1991-03-01 09:00   -0.288632
1991-06-01 09:00   -0.403570
1991-09-01 09:00    0.355567
1991-12-01 09:00   -1.138973
1992-03-01 09:00    0.006818
Freq: H, dtype: float64