In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from pandas import DataFrame, Series
from datetime import datetime
In [2]:
ts1 = Series(np.random.randn(3),
index=pd.date_range('2012-6-13', periods=3, freq='W-WED'))
ts1
Out[2]:
In [3]:
ts1.resample('B').mean()
Out[3]:
In [4]:
ts1.resample('D').ffill()
Out[4]:
In [5]:
dates = pd.DatetimeIndex(['2012-6-12', '2012-6-17', '2012-6-18',
'2012-6-21', '2012-6-22', '2012-6-29'])
ts2 = Series(np.random.randn(6), index=dates)
ts2
Out[5]:
In [6]:
ts1 = ts1.resample('D').ffill()
ts1.reindex(ts2.index, method='ffill')
Out[6]:
In [7]:
ts2 + ts1.reindex(ts2.index, method='ffill')
Out[7]:
In [8]:
gdp = Series([1.78, 1.94, 2.08, 2.01, 2.15, 2.31, 2.46],
index=pd.period_range('1984Q2', periods=7, freq='Q-SEP'))
infl = Series([0.025, 0.045, 0.037, 0.04],
index=pd.period_range('1982', periods=4, freq='A-DEC'))
gdp
Out[8]:
In [9]:
infl
Out[9]:
In [10]:
infl_q = infl.asfreq('Q-SEP', how='end')
infl_q
Out[10]:
In [11]:
infl_q.reindex(gdp.index, method='ffill')
Out[11]:
In [12]:
# 生成一个交易日内的日期范围和时间序列
rng = pd.date_range('2012-06-01 09:30', '2012-06-01 15:59', freq='T')
# 生成5天的时间点(9:30~15:59之间的值)
rng = rng.append([rng + pd.offsets.BDay(i) for i in range(1, 4)])
ts = Series(np.arange(len(rng), dtype=float), index=rng)
ts
Out[12]:
In [13]:
from datetime import time
ts[time(10, 0)]
Out[13]:
In [14]:
ts.at_time(time(10, 0))
Out[14]:
In [15]:
ts.between_time(time(10, 0), time(10, 1))
Out[15]:
In [16]:
# 将该时间序列的大部分内容随机设置为NA
indexer = np.sort(np.random.permutation(len(ts))[700:])
irr_ts = ts.copy()
irr_ts[indexer] = np.nan
irr_ts['2012-06-01 09:50':'2012-06-01 10:00']
Out[16]:
In [17]:
selection = pd.date_range('2012-06-01 10:00', periods=4, freq='B')
irr_ts.asof(selection)
Out[17]:
In [18]:
data1 = DataFrame(np.ones((6, 3), dtype=float),
columns=['a', 'b', 'c'],
index=pd.date_range('6/12/2012', periods=6))
data2 = DataFrame(np.ones((6, 3), dtype=float) * 2,
columns=['a', 'b', 'c'],
index=pd.date_range('6/13/2012', periods=6))
spliced = pd.concat([data1.ix[:'2012-06-14'], data2.ix['2012-06-15':]])
spliced
Out[18]:
In [20]:
pd.concat([data1, data2])
Out[20]:
In [22]:
data2 = DataFrame(np.ones((6, 4), dtype=float) * 2,
columns=['a', 'b', 'c', 'd'],
index=pd.date_range('6/13/2012', periods=6))
spliced = pd.concat([data1.ix[:'2012-06-14'], data2.ix['2012-06-15':]])
spliced
Out[22]:
In [23]:
spliced_filled = spliced.combine_first(data2)
spliced_filled
Out[23]:
In [24]:
spliced.update(data2, overwrite=False)
spliced
Out[24]:
In [25]:
cp_spliced = spliced.copy()
cp_spliced[['a', 'c']] = data1[['a', 'c']]
cp_spliced
Out[25]:
In [ ]:
In [ ]: