# -*- coding: utf-8 -*-
"""
Created on Fri May 24 11:26:09 2019
@author: User
"""
import pandas as pd
import numpy as np
dates=pd.date_range('2018-04-21','2018-05-10')
jd_ts=pd.Series(np.random.randn(20),dates)
print(jd_ts)
print("\n jd_ts['2018-05']:")
print(jd_ts['2018-05'])
print("\n jd_ts['2018-05-03':'2018-05-07']:")
print(jd_ts['2018-05-03':'2018-05-07'])
print("\n truncate(after='2018-04-26') 指定范围内的数据截取出来:")
print(jd_ts.truncate(after='2018-04-26'))
print("\n truncate(after='2018-04-26',before='2018-04-22') 指定范围内的数据截取出来:")
print(jd_ts.truncate(after='2018-04-26',before='2018-04-22'))
运行:
2018-04-21 -2.529867
2018-04-22 -1.031372
2018-04-23 0.132761
2018-04-24 -0.796262
2018-04-25 -0.551843
2018-04-26 -0.909600
2018-04-27 -1.295578
2018-04-28 0.022619
2018-04-29 -0.428454
2018-04-30 -0.862570
2018-05-01 -0.084349
2018-05-02 0.129956
2018-05-03 0.514946
2018-05-04 -1.725820
2018-05-05 -0.319413
2018-05-06 0.251834
2018-05-07 0.476161
2018-05-08 3.462132
2018-05-09 -0.729028
2018-05-10 -0.852085
Freq: D, dtype: float64
jd_ts['2018-05']:
2018-05-01 -0.084349
2018-05-02 0.129956
2018-05-03 0.514946
2018-05-04 -1.725820
2018-05-05 -0.319413
2018-05-06 0.251834
2018-05-07 0.476161
2018-05-08 3.462132
2018-05-09 -0.729028
2018-05-10 -0.852085
Freq: D, dtype: float64
jd_ts['2018-05-03':'2018-05-07']:
2018-05-03 0.514946
2018-05-04 -1.725820
2018-05-05 -0.319413
2018-05-06 0.251834
2018-05-07 0.476161
Freq: D, dtype: float64
truncate(after='2018-04-26') 指定范围内的数据截取出来:
2018-04-21 -2.529867
2018-04-22 -1.031372
2018-04-23 0.132761
2018-04-24 -0.796262
2018-04-25 -0.551843
2018-04-26 -0.909600
Freq: D, dtype: float64
truncate(after='2018-04-26',before='2018-04-22') 指定范围内的数据截取出来:
2018-04-22 -1.031372
2018-04-23 0.132761
2018-04-24 -0.796262
2018-04-25 -0.551843
2018-04-26 -0.909600
Freq: D, dtype: float64