python笔记:3.2.2.8pandas数据操作_时间序列索引与切片

# -*- 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
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值