Pandas时间序列和分组聚合

#时间序列
import pandas as pd import numpy as np # 生成一段时间范围 ''' 该函数主要用于生成一个固定频率的时间索引,在调用构造方法时,必须指定start、end、periods中的两个参数值,否则 报错。 时间序列频率: D 日历日的每天 B 工作日的每天 H 每小时 T或min 每分钟 S 每秒 L或ms U M BM MS BMS 每毫秒 每微秒 日历日的月底日期 工作日的月底日期 日历日的月初日期 工作日的月初日期 ''' date = pd.date_range(start='20190501',end='20190530') print(date) print("-"*20) #freq:日期偏移量,取值为string或DateOffset,默认为'D', freq='1h30min' freq='10D' # periods:固定时期,取值为整数或None date = pd.date_range(start='20190501',periods=10,freq='10D') print(date) print("-"*20) #时间序列在dataFrame中的作用 #可以将时间作为索引 index = pd.date_range(start='20190101',periods=10) df = pd.Series(np.random.randint(0,10,size = 10),index=index) print(df) print("-"*20) long_ts = pd.Series(np.random.randn(1000),index=pd.date_range('1/1/2019',periods=1000)) print(long_ts) print("-"*20) #根据年份获取 result = long_ts['2020'] print(result) print("-"*20) #年份和日期获取 result = long_ts['2020-05'] print(result) print("-"*20) #使用切片 result = long_ts['2020-05-01':'2020-05-06'] print(result) print("-"*20) #通过between_time()返回位于指定时间段的数据集 index=pd.date_range("2018-03-17","2018-03-30",freq="2H") ts = pd.Series(np.random.randn(157),index=index) print(ts.between_time("7:00","17:00")) print("-"*20) #这些操作也都适用于dataframe index=pd.date_range('1/1/2019',periods=100) df = pd.DataFrame(np.random.randn(100,4),index=index) print(df.loc['2019-04']) 输出: /Users/lazy/PycharmProjects/matplotlib/venv/bin/python /Users/lazy/PycharmProjects/matplotlib/drawing.py DatetimeIndex(['2019-05-01', '2019-05-02', '2019-05-03', '2019-05-04', '2019-05-05', '2019-05-06', '2019-05-07', '2019-05-08', '2019-05-09', '2019-05-10', '2019-05-11', '2019-05-12', '2019-05-13', '2019-05-14', '2019-05-15', '2019-05-16', '2019-05-17', '2019-05-18', '2019-05-19', '2019-05-20', '2019-05-21', '2019-05-22', '2019-05-23', '2019-05-24', '2019-05-25', '2019-05-26', '2019-05-27', '2019-05-28', '2019-05-29', '2019-05-30'], dtype='datetime64[ns]', freq='D') -------------------- DatetimeIndex(['2019-05-01', '2019-05-11', '2019-05-21', '2019-05-31', '2019-06-10', '2019-06-20', '2019-06-30', '2019-07-10', '2019-07-20', '2019-07-30'], dtype='datetime64[ns]', freq='10D') -------------------- 2019-01-01 9 2019-01-02 8 2019-01-03 9 2019-01-04 2 2019-01-05 4 2019-01-06 4 2019-01-07 0 2019-01-08 1 2019-01-09 4 2019-01-10 1 Freq: D, dtype: int64 -------------------- 2019-01-01 1.161118 2019-01-02 0.342857 2019-01-03 1.581292 2019-01-04 -0.928493 2019-01-05 -1.406328 ... 2021-09-22 0.106048 2021-09-23 0.228015 2021-09-24 -0.201558 2021-09-25 1.136008 2021-09-26 -0.947871 Freq: D, Length: 1000, dtype: float64 -------------------- 2020-01-01 1.828810 2020-01-02 1.425193 2020-01-03 -0.258607 2020-01-04 -0.390869 2020-01-05 -0.509062 ... 2020-12-27 0.155428 2020-12-28 -0.450071 2020-12-29 -0.050287 2020-12-30 0.033996 2020-12-31 -0.783760 Freq: D, Length: 366, dtype: float64 -------------------- 2020-05-01 0.843815 2020-05-02 -0.189866 2020-05-03 0.206807 2020-05-04 -0.279099 2020-05-05 0.575256 2020-05-06 -0.163009 2020-05-07 -0.850285 2020-05-08 -0.602792 2020-05-09 -0.630393 2020-05-10 -1.447383 2020-05-11 0.664726 2020-05-12 -0.108902 2020-05-13 0.333349 2020-05-14 1.068075 2020-05-15 -0.004767 2020-05-16 0.178172 2020-05-17 1.189467 2020-05-18 2.149068 2020-05-19 0.501122 2020-05-20 0.025200 2020-05-21 0.459819 2020-05-22 -0.688207 2020-05-23 -0.560723 2020-05-24 -0.448853 2020-05-25 0.612620 2020-05-26 0.781641 2020-05-27 0.225619 2020-05-28 -0.026749 2020-05-29 -0.020273 2020-05-30 0.812233 2020-05-31 -1.258738 Freq: D, dtype: float64 -------------------- 2020-05-01 0.843815 2020-05-02 -0.189866 2020-05-03 0.206807 2020-05-04 -0.279099 2020-05-05 0.575256 2020-05-06 -0.163009 Freq: D, dtype: float64 -------------------- 2018-03-17 08:00:00 0.704187 2018-03-17 10:00:00 0.496051 2018-03-17 12:00:00 1.828923 2018-03-17 14:00:00 -0.096337 2018-03-17 16:00:00 1.584530 ... 2018-03-29 08:00:00 0.779002 2018-03-29 10:00:00 -0.244056 2018-03-29 12:00:00 -0.428603 2018-03-29 14:00:00 1.297126 2018-03-29 16:00:00 0.482789 Length: 65, dtype: float64 -------------------- 0 1 2 3 2019-04-01 -2.074822 -0.939817 0.321402 -0.627823 2019-04-02 1.368356 0.150809 1.102027 -0.286527 2019-04-03 0.422506 -0.024193 -0.857528 1.061103 2019-04-04 -0.324066 -0.764358 -0.586841 1.520979 2019-04-05 1.398816 1.088023 -0.940833 1.249962 2019-04-06 -0.031951 0.905921 0.455782 -0.968012 2019-04-07 1.421253 -0.786199 0.875216 0.551437 2019-04-08 1.015066 -1.051041 0.430193 -0.014169 2019-04-09 0.279851 0.824598 -0.606735 -1.411600 2019-04-10 -0.252020 -0.408230 -0.698608 0.158843
 
 
import pandas as pd
import numpy as np

ts = pd.Series(np.random.randn(10),index=pd.date_range('1/1/2019',periods=10))
print(ts)
print("-"*20)
# 移动数据,索引不变,默认由NaN填充
# periods: 移动的位数 负数是向上移动
# fill_value: 移动后填充数据
print(ts.shift(periods=2,fill_value=100))
print("-"*20)
# 通过tshift()将索引移动指定的时间:
print(ts.tshift(2))
print("-"*20)
# 将时间戳转化成时间根式
print(pd.to_datetime(1554970740000,unit='ms'))
print("-"*20)
# utc是协调世界时,时区是以UTC的偏移量的形式表示的,但是注意设置utc=True,是让pandas对象具有时区性质,对于一列 进行转换的,会造成转换错误
# unit='ms' 设置粒度是到毫秒级别的
print(pd.to_datetime(1554970740000,unit='ms').tz_localize('UTC').tz_convert('Asia/Shanghai'))
print("-"*20)
# 处理一列
df = pd.DataFrame([1554970740000, 1554970800000, 1554970860000],columns = ['time_stamp'])
print(pd.to_datetime(df['time_stamp'],unit='ms').dt.tz_localize('UTC').dt.tz_convert('Asia/Shanghai')) #先赋予标准时区,再转换到东八区
print("-"*20)
# 处理中文
print(pd.to_datetime('2019年10月10日',format='%Y年%m月%d日'))
输出:
/Users/lazy/PycharmProjects/matplotlib/venv/bin/python /Users/lazy/PycharmProjects/matplotlib/drawing.py
2019-01-01 -2.679356
2019-01-02 0.775274
2019-01-03 -0.045711
2019-01-04 0.883532
2019-01-05 -0.941213
2019-01-06 -1.461701
2019-01-07 0.149344
2019-01-08 -0.185037
2019-01-09 -0.754532
2019-01-10 0.561909
Freq: D, dtype: float64
--------------------
2019-01-01 100.000000
2019-01-02 100.000000
2019-01-03 -2.679356
2019-01-04 0.775274
2019-01-05 -0.045711
2019-01-06 0.883532
2019-01-07 -0.941213
2019-01-08 -1.461701
2019-01-09 0.149344
2019-01-10 -0.185037
Freq: D, dtype: float64
--------------------
2019-01-03 -2.679356
2019-01-04 0.775274
2019-01-05 -0.045711
2019-01-06 0.883532
2019-01-07 -0.941213
2019-01-08 -1.461701
2019-01-09 0.149344
2019-01-10 -0.185037
2019-01-11 -0.754532
2019-01-12 0.561909
Freq: D, dtype: float64
--------------------
2019-04-11 08:19:00
--------------------
2019-04-11 16:19:00+08:00
--------------------
0 2019-04-11 16:19:00+08:00
1 2019-04-11 16:20:00+08:00
2 2019-04-11 16:21:00+08:00
Name: time_stamp, dtype: datetime64[ns, Asia/Shanghai]
--------------------
2019-10-10 00:00:00

猜你喜欢

转载自www.cnblogs.com/imcati/p/11294989.html