In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from pandas import DataFrame, Series

In [2]:
import datetime as datetime
from pandas.tseries.offsets import Hour, Minute
hour = Hour()
hour


Out[2]:
<Hour>

In [3]:
four_hours = Hour(4)
four_hours


Out[3]:
<4 * Hours>

In [4]:
pd.date_range('1/1/2000', '1/3/2000 23:59', freq='4h')


Out[4]:
DatetimeIndex(['2000-01-01 00:00:00', '2000-01-01 04:00:00',
               '2000-01-01 08:00:00', '2000-01-01 12:00:00',
               '2000-01-01 16:00:00', '2000-01-01 20:00:00',
               '2000-01-02 00:00:00', '2000-01-02 04:00:00',
               '2000-01-02 08:00:00', '2000-01-02 12:00:00',
               '2000-01-02 16:00:00', '2000-01-02 20:00:00',
               '2000-01-03 00:00:00', '2000-01-03 04:00:00',
               '2000-01-03 08:00:00', '2000-01-03 12:00:00',
               '2000-01-03 16:00:00', '2000-01-03 20:00:00'],
              dtype='datetime64[ns]', freq='4H')

In [5]:
Hour(2) + Minute(30)


Out[5]:
<150 * Minutes>

In [6]:
pd.date_range('1/1/2000', periods=10, freq='1h30min')


Out[6]:
DatetimeIndex(['2000-01-01 00:00:00', '2000-01-01 01:30:00',
               '2000-01-01 03:00:00', '2000-01-01 04:30:00',
               '2000-01-01 06:00:00', '2000-01-01 07:30:00',
               '2000-01-01 09:00:00', '2000-01-01 10:30:00',
               '2000-01-01 12:00:00', '2000-01-01 13:30:00'],
              dtype='datetime64[ns]', freq='90T')

In [7]:
rng = pd.date_range('1/1/2012', '9/1/2012', freq='WOM-3FRI')
list(rng)


Out[7]:
[Timestamp('2012-01-20 00:00:00', offset='WOM-3FRI'),
 Timestamp('2012-02-17 00:00:00', offset='WOM-3FRI'),
 Timestamp('2012-03-16 00:00:00', offset='WOM-3FRI'),
 Timestamp('2012-04-20 00:00:00', offset='WOM-3FRI'),
 Timestamp('2012-05-18 00:00:00', offset='WOM-3FRI'),
 Timestamp('2012-06-15 00:00:00', offset='WOM-3FRI'),
 Timestamp('2012-07-20 00:00:00', offset='WOM-3FRI'),
 Timestamp('2012-08-17 00:00:00', offset='WOM-3FRI')]

In [8]:
ts = Series(np.random.randn(4),
           index=pd.date_range('1/1/2000', periods=4, freq='M'))
ts


Out[8]:
2000-01-31    1.268283
2000-02-29   -0.688223
2000-03-31    0.925926
2000-04-30   -0.040020
Freq: M, dtype: float64

In [9]:
ts.shift(2)


Out[9]:
2000-01-31         NaN
2000-02-29         NaN
2000-03-31    1.268283
2000-04-30   -0.688223
Freq: M, dtype: float64

In [10]:
ts.shift(-2)


Out[10]:
2000-01-31    0.925926
2000-02-29   -0.040020
2000-03-31         NaN
2000-04-30         NaN
Freq: M, dtype: float64

In [11]:
ts / ts.shift(1) - 1


Out[11]:
2000-01-31         NaN
2000-02-29   -1.542641
2000-03-31   -2.345388
2000-04-30   -1.043222
Freq: M, dtype: float64

In [12]:
ts.shift(2, freq='M')


Out[12]:
2000-03-31    1.268283
2000-04-30   -0.688223
2000-05-31    0.925926
2000-06-30   -0.040020
Freq: M, dtype: float64

In [13]:
ts.shift(3, freq='D')


Out[13]:
2000-02-03    1.268283
2000-03-03   -0.688223
2000-04-03    0.925926
2000-05-03   -0.040020
dtype: float64

In [14]:
ts.shift(1, freq='3D')


Out[14]:
2000-02-03    1.268283
2000-03-03   -0.688223
2000-04-03    0.925926
2000-05-03   -0.040020
dtype: float64

In [15]:
ts.shift(1, freq='90T')


Out[15]:
2000-01-31 01:30:00    1.268283
2000-02-29 01:30:00   -0.688223
2000-03-31 01:30:00    0.925926
2000-04-30 01:30:00   -0.040020
Freq: M, dtype: float64

通过偏移量对日期进行位移


In [16]:
from datetime import datetime
from pandas.tseries.offsets import Day, MonthEnd
now = datetime(2011, 11, 17)
now + 3 * Day()


Out[16]:
Timestamp('2011-11-20 00:00:00')

In [17]:
now + MonthEnd()


Out[17]:
Timestamp('2011-11-30 00:00:00')

In [18]:
now + MonthEnd(2)


Out[18]:
Timestamp('2011-12-31 00:00:00')

In [19]:
now + 2 * MonthEnd()


Out[19]:
Timestamp('2011-12-31 00:00:00')

In [20]:
offset = MonthEnd()
offset.rollforward(now)


Out[20]:
Timestamp('2011-11-30 00:00:00')

In [21]:
offset.rollback(now)


Out[21]:
Timestamp('2011-10-31 00:00:00')

In [22]:
ts = Series(np.random.randn(20),
           index=pd.date_range('1/15/2000', periods=20, freq='4d'))
ts.groupby(offset.rollforward).mean()


Out[22]:
2000-01-31   -0.498757
2000-02-29    0.040570
2000-03-31    0.654397
dtype: float64

In [23]:
ts.resample('M').mean()


Out[23]:
2000-01-31   -0.498757
2000-02-29    0.040570
2000-03-31    0.654397
Freq: M, dtype: float64

时区


In [24]:
import pytz
pytz.common_timezones[-5:]


Out[24]:
['US/Eastern', 'US/Hawaii', 'US/Mountain', 'US/Pacific', 'UTC']

In [25]:
tz = pytz.timezone('US/Eastern')
tz


Out[25]:
<DstTzInfo 'US/Eastern' LMT-1 day, 19:04:00 STD>

In [26]:
rng = pd.date_range('3/9/2012 9:30', periods=6, freq='D')
ts = Series(np.random.randn(len(rng)), index=rng)
print(ts.index.tz)


None

In [27]:
pd.date_range('3/9/2012 9:30', periods=10, freq='D', tz='UTC')


Out[27]:
DatetimeIndex(['2012-03-09 09:30:00+00:00', '2012-03-10 09:30:00+00:00',
               '2012-03-11 09:30:00+00:00', '2012-03-12 09:30:00+00:00',
               '2012-03-13 09:30:00+00:00', '2012-03-14 09:30:00+00:00',
               '2012-03-15 09:30:00+00:00', '2012-03-16 09:30:00+00:00',
               '2012-03-17 09:30:00+00:00', '2012-03-18 09:30:00+00:00'],
              dtype='datetime64[ns, UTC]', freq='D')

In [28]:
ts_utc = ts.tz_localize('UTC')
ts_utc


Out[28]:
2012-03-09 09:30:00+00:00   -0.463406
2012-03-10 09:30:00+00:00   -0.583775
2012-03-11 09:30:00+00:00    0.986700
2012-03-12 09:30:00+00:00    1.167509
2012-03-13 09:30:00+00:00   -0.128039
2012-03-14 09:30:00+00:00    1.763953
Freq: D, dtype: float64

In [29]:
ts_utc.index


Out[29]:
DatetimeIndex(['2012-03-09 09:30:00+00:00', '2012-03-10 09:30:00+00:00',
               '2012-03-11 09:30:00+00:00', '2012-03-12 09:30:00+00:00',
               '2012-03-13 09:30:00+00:00', '2012-03-14 09:30:00+00:00'],
              dtype='datetime64[ns, UTC]', freq='D')

In [30]:
ts_utc.tz_convert('US/Eastern')


Out[30]:
2012-03-09 04:30:00-05:00   -0.463406
2012-03-10 04:30:00-05:00   -0.583775
2012-03-11 05:30:00-04:00    0.986700
2012-03-12 05:30:00-04:00    1.167509
2012-03-13 05:30:00-04:00   -0.128039
2012-03-14 05:30:00-04:00    1.763953
Freq: D, dtype: float64

In [31]:
ts_eastern = ts.tz_localize('US/Eastern')
ts_eastern.tz_convert('UTC')


Out[31]:
2012-03-09 14:30:00+00:00   -0.463406
2012-03-10 14:30:00+00:00   -0.583775
2012-03-11 13:30:00+00:00    0.986700
2012-03-12 13:30:00+00:00    1.167509
2012-03-13 13:30:00+00:00   -0.128039
2012-03-14 13:30:00+00:00    1.763953
Freq: D, dtype: float64

In [32]:
ts_eastern.tz_convert('Europe/Berlin')


Out[32]:
2012-03-09 15:30:00+01:00   -0.463406
2012-03-10 15:30:00+01:00   -0.583775
2012-03-11 14:30:00+01:00    0.986700
2012-03-12 14:30:00+01:00    1.167509
2012-03-13 14:30:00+01:00   -0.128039
2012-03-14 14:30:00+01:00    1.763953
Freq: D, dtype: float64

In [33]:
ts_eastern.tz_convert('Asia/Shanghai')


Out[33]:
2012-03-09 22:30:00+08:00   -0.463406
2012-03-10 22:30:00+08:00   -0.583775
2012-03-11 21:30:00+08:00    0.986700
2012-03-12 21:30:00+08:00    1.167509
2012-03-13 21:30:00+08:00   -0.128039
2012-03-14 21:30:00+08:00    1.763953
Freq: D, dtype: float64

In [34]:
ts.index.tz_localize('Asia/Shanghai')


Out[34]:
DatetimeIndex(['2012-03-09 09:30:00+08:00', '2012-03-10 09:30:00+08:00',
               '2012-03-11 09:30:00+08:00', '2012-03-12 09:30:00+08:00',
               '2012-03-13 09:30:00+08:00', '2012-03-14 09:30:00+08:00'],
              dtype='datetime64[ns, Asia/Shanghai]', freq='D')

In [35]:
stamp = pd.Timestamp('2011-03-12 04:00')
stamp_utc = stamp.tz_localize('utc')
stamp_utc.tz_convert('US/Eastern')


Out[35]:
Timestamp('2011-03-11 23:00:00-0500', tz='US/Eastern')

In [36]:
stamp_moscow = pd.Timestamp('2011-03-12 04:00', tz='Europe/Moscow')
stamp_moscow


Out[36]:
Timestamp('2011-03-12 04:00:00+0300', tz='Europe/Moscow')

In [37]:
stamp_utc.value


Out[37]:
1299902400000000000L

In [38]:
stamp_utc.tz_convert('US/Eastern').value


Out[38]:
1299902400000000000L

In [39]:
# 夏令时转变前30分钟
from pandas.tseries.offsets import Hour
stamp = pd.Timestamp('2012-03-11 01:30', tz='US/Eastern')
stamp


Out[39]:
Timestamp('2012-03-11 01:30:00-0500', tz='US/Eastern')

In [40]:
stamp + Hour()


Out[40]:
Timestamp('2012-03-11 03:30:00-0400', tz='US/Eastern')

In [41]:
# 夏令时转变前90分钟
stamp = pd.Timestamp('2012-11-04 00:30', tz='US/Eastern')
stamp


Out[41]:
Timestamp('2012-11-04 00:30:00-0400', tz='US/Eastern')

In [42]:
stamp + 2 * Hour()


Out[42]:
Timestamp('2012-11-04 01:30:00-0500', tz='US/Eastern')

In [43]:
rng = pd.date_range('3/7/2012 9:30', periods=10, freq='B')
ts = Series(np.random.randn(len(rng)), index=rng)
ts


Out[43]:
2012-03-07 09:30:00    1.847267
2012-03-08 09:30:00    0.981012
2012-03-09 09:30:00    0.725554
2012-03-12 09:30:00    0.606718
2012-03-13 09:30:00    0.152469
2012-03-14 09:30:00   -0.627997
2012-03-15 09:30:00    1.351002
2012-03-16 09:30:00   -1.670083
2012-03-19 09:30:00   -0.736366
2012-03-20 09:30:00    0.784309
Freq: B, dtype: float64

In [44]:
ts1 = ts[:7].tz_localize('Europe/London')
ts2 = ts1[2:].tz_convert('Europe/Moscow')
result = ts1 + ts2
result.index


Out[44]:
DatetimeIndex(['2012-03-07 09:30:00+00:00', '2012-03-08 09:30:00+00:00',
               '2012-03-09 09:30:00+00:00', '2012-03-12 09:30:00+00:00',
               '2012-03-13 09:30:00+00:00', '2012-03-14 09:30:00+00:00',
               '2012-03-15 09:30:00+00:00'],
              dtype='datetime64[ns, UTC]', freq='B')

In [45]:
p = pd.Period(2007, freq='A-DEC')
p


Out[45]:
Period('2007', 'A-DEC')

In [46]:
p+5


Out[46]:
Period('2012', 'A-DEC')

In [47]:
p - 2


Out[47]:
Period('2005', 'A-DEC')

In [48]:
pd.Period('2014', freq='A-DEC') - p


Out[48]:
7L

In [49]:
rng = pd.period_range('1/1/2000', '6/30/2000', freq='M')
rng


Out[49]:
PeriodIndex(['2000-01', '2000-02', '2000-03', '2000-04', '2000-05', '2000-06'], dtype='int64', freq='M')

In [50]:
Series(np.random.randn(6), index=rng)


Out[50]:
2000-01   -1.367498
2000-02   -2.507039
2000-03    0.356113
2000-04    0.939846
2000-05   -1.222235
2000-06   -0.970231
Freq: M, dtype: float64

In [51]:
values = ['2001Q3', '2002Q2', '2003Q1']
index = pd.PeriodIndex(values, freq='Q-DEC')
index


Out[51]:
PeriodIndex(['2001Q3', '2002Q2', '2003Q1'], dtype='int64', freq='Q-DEC')

In [52]:
p = pd.Period('2007', freq='A-DEC')
p.asfreq('M', how='start')


Out[52]:
Period('2007-01', 'M')

In [53]:
p.asfreq('M', how='end')


Out[53]:
Period('2007-12', 'M')

In [54]:
p = pd.Period('2007', freq='A-JUN')
p.asfreq('M', 'start')


Out[54]:
Period('2006-07', 'M')

In [55]:
p.asfreq('M', 'end')


Out[55]:
Period('2007-06', 'M')

In [56]:
p = pd.Period('2007-08', 'M')
p.asfreq('A-JUN')


Out[56]:
Period('2008', 'A-JUN')

In [57]:
rng = pd.period_range('2006', '2009', freq='A-DEC')
ts = Series(np.random.randn(len(rng)), index=rng)
ts


Out[57]:
2006    0.115057
2007    1.381682
2008    0.196668
2009   -1.867623
Freq: A-DEC, dtype: float64

In [58]:
ts.asfreq('M', how='start')


Out[58]:
2006-01    0.115057
2007-01    1.381682
2008-01    0.196668
2009-01   -1.867623
Freq: M, dtype: float64

In [59]:
ts.asfreq('B', how='end')


Out[59]:
2006-12-29    0.115057
2007-12-31    1.381682
2008-12-31    0.196668
2009-12-31   -1.867623
Freq: B, dtype: float64

In [60]:
p = pd.Period('2012Q4', freq='Q-JAN')
p


Out[60]:
Period('2012Q4', 'Q-JAN')

In [61]:
p.asfreq('D', 'start')


Out[61]:
Period('2011-11-01', 'D')

In [62]:
p.asfreq('D', 'end')


Out[62]:
Period('2012-01-31', 'D')

In [63]:
p4pm = (p.asfreq('B', 'e') - 1).asfreq('T', 's') + 16 * 60
p4pm


Out[63]:
Period('2012-01-30 16:00', 'T')

In [64]:
p4pm.to_timestamp()


Out[64]:
Timestamp('2012-01-30 16:00:00')

In [65]:
rng = pd.period_range('2011Q3', '2012Q4', freq='Q-JAN')
ts = Series(np.arange(len(rng)), index=rng)
ts


Out[65]:
2011Q3    0
2011Q4    1
2012Q1    2
2012Q2    3
2012Q3    4
2012Q4    5
Freq: Q-JAN, dtype: int32

In [66]:
new_rng = (rng.asfreq('B', 'e') - 1).asfreq('T', 's') + 16 * 60
ts.index = new_rng.to_timestamp()
ts


Out[66]:
2010-10-28 16:00:00    0
2011-01-28 16:00:00    1
2011-04-28 16:00:00    2
2011-07-28 16:00:00    3
2011-10-28 16:00:00    4
2012-01-30 16:00:00    5
dtype: int32

In [71]:
rng = pd.date_range('1/1/2000', periods=3, freq='M')
ts = Series(np.random.randn(3), index=rng)
pts = ts.to_period()
ts


Out[71]:
2000-01-31    1.579485
2000-02-29   -2.749163
2000-03-31   -0.245995
Freq: M, dtype: float64

In [72]:
pts


Out[72]:
2000-01    1.579485
2000-02   -2.749163
2000-03   -0.245995
Freq: M, dtype: float64

In [73]:
rng = pd.date_range('1/29/2000', periods=6, freq='D')
ts2 = Series(np.random.randn(6), index=rng)
ts2.to_period('M')


Out[73]:
2000-01   -0.415081
2000-01   -1.112510
2000-01    0.018112
2000-02   -0.463518
2000-02    0.231962
2000-02   -0.974565
Freq: M, dtype: float64

In [74]:
ts2.to_period()


Out[74]:
2000-01-29   -0.415081
2000-01-30   -1.112510
2000-01-31    0.018112
2000-02-01   -0.463518
2000-02-02    0.231962
2000-02-03   -0.974565
Freq: D, dtype: float64

In [75]:
ts2.to_period('H')


Out[75]:
2000-01-29 00:00   -0.415081
2000-01-30 00:00   -1.112510
2000-01-31 00:00    0.018112
2000-02-01 00:00   -0.463518
2000-02-02 00:00    0.231962
2000-02-03 00:00   -0.974565
Freq: H, dtype: float64

In [76]:
pts = ts.to_period()
pts


Out[76]:
2000-01    1.579485
2000-02   -2.749163
2000-03   -0.245995
Freq: M, dtype: float64

In [77]:
pts.to_timestamp(how='end')


Out[77]:
2000-01-31    1.579485
2000-02-29   -2.749163
2000-03-31   -0.245995
Freq: M, dtype: float64

In [ ]: