我想使用特定日期(或月份)作为第一个 bin 的边缘对 Pandas 对象重新采样。例如,在下面的代码片段中,我希望我的第一个索引值是2020-02-29,我很乐意指定start=2or start="2020-02-29"。
>>> dates = pd.date_range("2020-01-29", "2021-07-04")
>>> s = pd.Series(range(len(dates)), index=dates)
>>> s.resample('4M').count()
2020-01-31 3
2020-05-31 121
2020-09-30 122
2021-01-31 123
2021-05-31 120
2021-09-30 34
Freq: 4M, dtype: int64
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我能想到的最干净的用途,pd.cut并且groupby:
>>> rule = "4M"
>>> start = pd.Timestamp("2020-02-29") - pd.tseries.frequencies.to_offset(rule)
>>> end = s.index.max() + pd.tseries.frequencies.to_offset(rule)
>>> bins = pd.date_range(start, end, freq=rule)
>>> gb = s.groupby(pd.cut(s.index, bins)).count()
>>> gb.index = gb.index.categories.right
>>> gb
2020-02-29 32
2020-06-30 …Run Code Online (Sandbox Code Playgroud) 对数据帧进行重新采样可以使数据帧达到更高或更低的时间分辨率。大多数时候,这用于降低分辨率(例如,将 1 分钟数据重新采样为每月值)。当数据集稀疏时(例如,2020 年 2 月没有收集任何数据),2020 年 2 月的行将填充重采样数据帧的 NaN。问题是,当数据记录很长且稀疏时,会有大量 NaN 行,这使得数据帧不必要地变大并占用大量 CPU 时间。例如,考虑这个数据帧和重新采样操作:
import numpy as np
import pandas as pd
freq1 = pd.date_range("20000101", periods=10, freq="S")
freq2 = pd.date_range("20200101", periods=10, freq="S")
index = np.hstack([freq1.values, freq2.values])
data = np.random.randint(0, 100, (20, 10))
cols = list("ABCDEFGHIJ")
df = pd.DataFrame(index=index, data=data, columns=cols)
# now resample to daily average
df = df.resample(rule="1D").mean()
Run Code Online (Sandbox Code Playgroud)
该数据框中的大部分数据都是无用的,可以通过以下方式删除:
df.dropna(how="all", axis=0, inplace=True)
Run Code Online (Sandbox Code Playgroud)
然而,这是草率的。是否有另一种方法来重新采样数据帧,该方法不会用 NaN 填充所有数据间隙(即在上面的示例中,生成的数据帧将只有两行)?
我是node.js的新手,我正在寻找一种模仿pandas.resample(\xe2\x80\x983H\xe2\x80\x99 , label=\xe2\x80\x99right\xe2\x80\x99 , close=\xe2\x80\x99left\xe2\x80\x99).max() 使用时间序列,能够获得类似数据帧的结构,其在 3 小时的块中发生的最大值(每 3 小时 1 个值) ,最大的一个)。我也有兴趣获取 20 分钟或更细粒度的数据。
\n我正在danfo.js https://danfo.jsdata.org/api-reference/dataframe中探索数据帧,但我没有看到任何与重新采样具有相同功能的东西。
\n请问你能帮我吗?谢谢你!
\n我有两个单独的时间序列pandas.dataframe,第一个 -series1条目较少,起始数据时间与第二个不同 - series2:
index1 = pd.date_range(start='2020-06-16 23:16:00', end='2020-06-16 23:40:30', freq='1T')
series1 = pd.Series(range(len(index1)), index=index1)
index2 = pd.date_range('2020-06-16 23:15:00', end='2020-06-16 23:50:30', freq='30S')
series2 = pd.Series(range(len(index2)), index=index2)
Run Code Online (Sandbox Code Playgroud)
我怎样才能重采样系列2匹配DatetimeIndex的series1?
我有一个带有多索引的数据框:“主题”和“日期时间”。每行对应一个主题和一个日期时间,数据帧的列对应各种测量值。
每个科目的天数范围不同,并且给定科目可能缺少某些天数(参见示例)。此外,对于给定的一天,一个主题可以有一个或多个值。
我想重新采样数据框,以便:
例如,以下数据框示例:
a b
subject datetime
patient1 2018-01-01 00:00:00 2.0 high
2018-01-01 01:00:00 NaN medium
2018-01-01 02:00:00 6.0 NaN
2018-01-01 03:00:00 NaN NaN
2018-01-02 00:00:00 4.3 low
patient2 2018-01-01 00:00:00 NaN medium
2018-01-01 02:00:00 NaN NaN
2018-01-01 03:00:00 5.0 NaN
2018-01-03 00:00:00 9.0 NaN
2018-01-04 02:00:00 NaN NaN
Run Code Online (Sandbox Code Playgroud)
应该返回:
a b
subject datetime
patient1 2018-01-01 00:00:00 6.0 medium
2018-01-02 00:00:00 4.3 low
patient2 2018-01-01 00:00:00 5.0 medium
2018-01-03 00:00:00 9.0 NaN
Run Code Online (Sandbox Code Playgroud)
我花了太多时间尝试使用带有 'pad' …
我想将pandas.Series每小时DatetimeIndex到每月的值聚合起来 - 同时考虑到午夜的偏移。
考虑以下跨度约为 1.5 个月的(统一)时间序列。
import pandas as pd
hours = pd.Series(1, pd.date_range('2020-02-23 06:00', freq = 'H', periods=1008))
hours
# 2020-02-23 06:00:00 1
# 2020-02-23 07:00:00 1
# ..
# 2020-04-05 04:00:00 1
# 2020-04-05 05:00:00 1
# Freq: H, Length: 1000, dtype: int64
Run Code Online (Sandbox Code Playgroud)
我想将这些总结为几个月,同时考虑到在此用例中这些日子从 06:00 开始。结果应该是:
2020-02-01 06:00:00 168
2020-03-01 06:00:00 744
2020-04-01 06:00:00 96
freq: MS, dtype: int64
Run Code Online (Sandbox Code Playgroud)
我怎么做??
我可以在考虑偏移量的同时使用以下offset参数聚合到天数:
2020-02-01 06:00:00 168
2020-03-01 06:00:00 …Run Code Online (Sandbox Code Playgroud)这就是我所拥有的:
df = pd.DataFrame({'item': [1,1,2,2,1,1],
'shop': ['A','A','A','A','B','B'],
'date': pd.to_datetime(['2018.01.'+ str(x) for x in [2,3,1,4,4,5]]),
'qty': [5,6,7,8,9,10]})
print(df)
item shop date qty
0 1 A 2018-01-02 5
1 1 A 2018-01-03 6
2 2 A 2018-01-01 7
3 2 A 2018-01-04 8
4 1 B 2018-01-04 9
5 1 B 2018-01-05 10
Run Code Online (Sandbox Code Playgroud)
这就是我要的:
out = pd.DataFrame({'item': [1,1,1,1,2,2,2,2,2,1,1],
'shop': ['A','A','A','A','A','A','A','A','A','B','B'],
'date': pd.to_datetime(['2018.01.'+ str(x) for x in [2,3,4,5,1,2,3,4,5,4,5]]),
'qty': [5,6,0,0,7,0,0,8,0,9,10]})
print(out)
item shop date qty
0 1 A 2018-01-02 5
1 1 A …Run Code Online (Sandbox Code Playgroud) 我有一个带有时间序列间隙的熊猫数据框。
它看起来像下面这样:
输入示例
--------------------------------------
Timestamp Close
2021-02-07 09:30:00 124.624
2021-02-07 09:31:00 124.617
2021-02-07 10:04:00 123.946
2021-02-07 16:00:00 123.300
2021-02-09 09:04:00 125.746
2021-02-09 09:05:00 125.646
2021-02-09 15:58:00 125.235
2021-02-09 15:59:00 126.987
2021-02-09 16:00:00 127.124
Run Code Online (Sandbox Code Playgroud)
所需输出
--------------------------------------------
Timestamp Close
2021-02-07 09:30:00 124.624
2021-02-07 09:31:00 124.617
2021-02-07 09:32:00 124.617
2021-02-07 09:33:00 124.617
'Insert a line for each minute up to the next available
timestamp with the Close value form the last available timestamp'
2021-02-07 10:03:00 124.617
2021-02-07 10:04:00 123.946
2021-02-07 16:00:00 123.300
'I …Run Code Online (Sandbox Code Playgroud) pandas ×7
python ×7
time-series ×4
dataframe ×3
datetime ×2
danfojs ×1
date ×1
nan ×1
node.js ×1
resampling ×1