Python 按周分组的时间分布
我想找到timeseries数据按周分组的时间分布。例如,timeseries是:Python 按周分组的时间分布,python,pandas,dataframe,datetime,time-series,Python,Pandas,Dataframe,Datetime,Time Series,我想找到timeseries数据按周分组的时间分布。例如,timeseries是: 2019-04-01 02:00:00 0.6 2019-04-02 10:45:00 2.0 2019-04-03 02:00:00 3.0 2019-04-10 00:00:00 0.6 2019-04-11 10:45:00 2.0 2019-04-13 10:45:00 6.0 2019-04-17 11:45:00 2.5 2019-04-18 11:45:00
2019-04-01 02:00:00 0.6
2019-04-02 10:45:00 2.0
2019-04-03 02:00:00 3.0
2019-04-10 00:00:00 0.6
2019-04-11 10:45:00 2.0
2019-04-13 10:45:00 6.0
2019-04-17 11:45:00 2.5
2019-04-18 11:45:00 3.0
2019-04-19 11:45:00 6.0
dtype: float64
我想知道的是,在第14周(2019-04-01的周),在02:00:00
有两条记录,在10:45:00
有一条记录,其他时间没有记录。在第15周,在00:00:00
有一条记录,在10:45:00
有两条记录,其他时间没有记录
这是目前我的解决方案,用于查找以15分钟为增量的分布:
import panda as pd
import numpy as np
import datetime as dt
def dist(series, bins):
h = np.histogram(series, bins)
return dict(zip(h[1][:-1], h[0]))
# creating bins, i.e. 15min increments throughout the day
times = pd.Series(index = pd.date_range(start='2019-01-01', end='2019-01-02', freq='15min'))
times = set(times.index.time)
times = list(times)
times.sort()
dummy = (dt.datetime.combine(dt.date.today(), max(times))+dt.timedelta(seconds = 10)).time()
times = times + [dummy]
# finding distribution each week
df = pd.DataFrame({'week': list(timeseries.index.week), 'time': list(timeseries.index.time)})
df = df.groupby(by=['week'])['time'].apply(lambda x: dist(x, times))
df.index.names = ['week', 'time']
df.name = 'counts'
df = df.reset_index().pivot(index='time', columns='week', values='counts')
有更好的方法吗?像这样简单的事情怎么样
#我从一个系列开始
s、 总目(2)
#时间
# 2019-04-01 02:00:00 0.6
# 2019-04-02 10:45:00 2.0
#名称:value,数据类型:float64
#将序列重新采样到预期的bin,例如15分钟
#用未定义的值填充
s=s.重新采样('15分钟')。asfreq()
s、 总目(3)
#时间
# 2019-04-01 02:00:00 0.6
#2019-04-01 02:15:00南
#2019-04-01 02:30:00南
#频率:15T,名称:value,数据类型:float64
#执行摘要以获取按周/时间定义的次数
#按15分钟取样(不计算NaN)
结果=s.groupby([s.index.week,s.index.time]).count()
结果.总目()
#时间
# 14 00:00:00 0
# 00:15:00 0
# 00:30:00 0
# 00:45:00 0
# 01:00:00 0
#名称:value,数据类型:int64
#仅获取具有值的小时数
结果[结果!=0]
#时间
# 14 02:00:00 2
# 10:45:00 1
# 15 00:00:00 1
# 10:45:00 2
# 16 11:45:00 3
#名称:value,数据类型:int64
我想它能给你想要的答案
想知道在第14周(2019-04-01
)有两条记录在02:00:00
,一条记录在10:45:00
,其他时间没有记录。在第15周,在00:00:00
有一条记录,在10:45:00
有两条记录,其他时间没有记录
笔记
这是如何生成示例DataFrame
将熊猫作为pd导入
输入io
zz=”“”
时间、价值
2019-04-01 02:00:00, 0.6
2019-04-02 10:45:00, 2.0
2019-04-03 02:00:00, 3.0
2019-04-10 00:00:00, 0.6
2019-04-11 10:45:00, 2.0
2019-04-13 10:45:00, 6.0
2019-04-17 11:45:00, 2.5
2019-04-18 11:45:00, 3.0
2019-04-19 11:45:00, 6.0"""
df=pd.read_表(io.StringIO(zz),sep=',')
df=df.set_索引(pd.DatetimeIndex(df['time']))
df=df.drop('时间',轴=1)
s=df.iloc[:,0]
像这样简单的事情怎么样
#我从一个系列开始
s、 总目(2)
#时间
# 2019-04-01 02:00:00 0.6
# 2019-04-02 10:45:00 2.0
#名称:value,数据类型:float64
#将序列重新采样到预期的bin,例如15分钟
#用未定义的值填充
s=s.重新采样('15分钟')。asfreq()
s、 总目(3)
#时间
# 2019-04-01 02:00:00 0.6
#2019-04-01 02:15:00南
#2019-04-01 02:30:00南
#频率:15T,名称:value,数据类型:float64
#执行摘要以获取按周/时间定义的次数
#按15分钟取样(不计算NaN)
结果=s.groupby([s.index.week,s.index.time]).count()
结果.总目()
#时间
# 14 00:00:00 0
# 00:15:00 0
# 00:30:00 0
# 00:45:00 0
# 01:00:00 0
#名称:value,数据类型:int64
#仅获取具有值的小时数
结果[结果!=0]
#时间
# 14 02:00:00 2
# 10:45:00 1
# 15 00:00:00 1
# 10:45:00 2
# 16 11:45:00 3
#名称:value,数据类型:int64
我想它能给你想要的答案
想知道在第14周(2019-04-01
)有两条记录在02:00:00
,一条记录在10:45:00
,其他时间没有记录。在第15周,在00:00:00
有一条记录,在10:45:00
有两条记录,其他时间没有记录
笔记
这是如何生成示例DataFrame
将熊猫作为pd导入
输入io
zz=”“”
时间、价值
2019-04-01 02:00:00, 0.6
2019-04-02 10:45:00, 2.0
2019-04-03 02:00:00, 3.0
2019-04-10 00:00:00, 0.6
2019-04-11 10:45:00, 2.0
2019-04-13 10:45:00, 6.0
2019-04-17 11:45:00, 2.5
2019-04-18 11:45:00, 3.0
2019-04-19 11:45:00, 6.0"""
df=pd.read_表(io.StringIO(zz),sep=',')
df=df.set_索引(pd.DatetimeIndex(df['time']))
df=df.drop('时间',轴=1)
s=df.iloc[:,0]
谢谢,这很有帮助!如果我们想在一天中的其他时间使用零呢?也就是说,分发的显式垃圾箱?@好奇的是,我修改了我的答案,以集成重采样。希望它能符合您的期望。谢谢,这很有帮助!如果我们想在一天中的其他时间使用零呢?也就是说,分发的显式垃圾箱?@好奇的是,我修改了我的答案,以集成重采样。希望它能符合你的期望。