问题
我需要将 DataFrame 的长度减少到某个外部定义的整数(可能是两行、10,000 行等,但总长度会减少),但我也想保留生成的 DataFrame 代表原始数据.原始数据帧(我们称之为 df
)有一个 datetime
列 ( utc_time
) 和数据值列 ( data_value
)。日期时间始终是连续的、不重复的,但间隔不均匀(即数据可能“丢失”)。对于此示例中的 DataFrame,时间戳以十分钟为间隔(当数据存在时)。
尝试
为了实现这一点,我立即按照以下逻辑进行重采样:找到第一个和最后一个时间戳之间的秒数差,将其除以所需的最终长度,这就是重采样因子。我在这里设置:
# Define the desired final length.
final_length = 2
# Define the first timestamp.
first_timestamp = df['utc_time'].min().timestamp()
# Define the last timestamp.
last_timestamp = df['utc_time'].max().timestamp()
# Define the difference in seconds between the first and last timestamps.
delta_t = last_timestamp - first_timestamp
# Define the resampling factor.
resampling_factor = np.ceil(delta_t / final_length)
# Set the index from the `utc_time` column so that we can resample nicely.
df.set_index('utc_time', drop=True, inplace=True)
# Do the resampling.
resamp = df.resample(f'{resampling_factor}S')
看resamp
,我只是循环并打印:for i in resamp:
print(i)
这产生了(我做了一些清理)以下内容: utc_time data_value
2016-09-28 21:10:00 140.0
2016-09-28 21:20:00 250.0
2016-09-28 21:30:00 250.0
2016-09-28 21:40:00 240.0
2016-09-28 21:50:00 240.0
... ...
2018-08-06 13:00:00 240.0
2018-08-06 13:10:00 240.0
2018-08-06 13:20:00 240.0
2018-08-06 13:30:00 240.0
2018-08-06 13:40:00 230.0
[69889 rows x 1 columns])
utc_time data_value
2018-08-06 13:50:00 230.0
2018-08-06 14:00:00 230.0
2018-08-06 14:10:00 230.0
2018-08-06 14:20:00 230.0
2018-08-06 14:30:00 230.0
... ...
2020-06-14 02:50:00 280.0
2020-06-14 03:00:00 280.0
2020-06-14 03:10:00 280.0
2020-06-14 03:20:00 280.0
2020-06-14 03:30:00 280.0
[97571 rows x 1 columns])
utc_time data_value
2020-06-14 03:40:00 280.0
2020-06-14 03:50:00 280.0
2020-06-14 04:00:00 280.0
2020-06-14 04:10:00 280.0
2020-06-14 04:20:00 280.0
... ...
2020-06-15 00:10:00 280.0
2020-06-15 00:20:00 270.0
2020-06-15 00:30:00 270.0
2020-06-15 00:40:00 270.0
2020-06-15 00:50:00 280.0
[128 rows x 1 columns])
正如人们所见,这产生了三个垃圾箱,而不是我预期的两个。我可以做一些不同的事情,比如改变我选择重采样因子的方式(例如,找到时间戳之间的平均时间,然后乘以(DataFrame 的长度/
final_length
)应该会产生一个更保守的重采样因子),但这会,在我看来,成为潜在问题的面具。主要是,我很想了解为什么会发生这种情况。这导致...题
有谁知道为什么会发生这种情况,我可能会采取哪些步骤来确保我们获得所需数量的垃圾箱?我想知道这是否是一个偏移问题——也就是说,尽管我们将第一个 bin 中的第一个时间戳视为来自 DataFrame 的第一个时间戳,但也许 Pandas 实际上在此之前启动了 bin?
对于任何想在家玩的人来说,测试 DataFrame 可以是 found here作为.csv。要将其作为 DataFrame 输入:
df = pd.read_csv('test.csv', parse_dates=[0])
最佳答案
概括
df.resample()
创建的垃圾箱将仅在一端(左侧或右侧)关闭。使用“1.”中列出的选项之一修复此问题。kind='period'
修复它作为 resample()
的参数. (见“3”)1. 看一眼输入数据(以及我们需要什么样的垃圾箱)
输入数据来自
2016-09-28 21:10:00
至 2020-06-15 00:50:00
,并使用 resampling_factor
你有,我们得到:In [63]: df.index.min()
Out[63]: Timestamp('2016-09-28 21:10:00')
In [64]: df.index.min() + pd.Timedelta(f'{resampling_factor}S')
Out[64]: Timestamp('2018-08-07 11:00:00')
In [65]: _ + pd.Timedelta(f'{resampling_factor}S')
Out[65]: Timestamp('2020-06-15 00:50:00')
要使用这些时间戳将数据分成两部分,我们需要 bins['2016-09-28 21:10:00', '2018-08-07 11:00:00')
['2018-08-07 11:00:00', '2020-06-15 00:50:00']
(
[
表示封闭端,(
表示开放端)closed='left'|'right'
,)。与 closed='left'
你将会拥有['2016-09-28 21:10:00', '2018-08-07 11:00:00')
['2018-08-07 11:00:00', '2020-06-15 00:50:00')
['2020-06-15 00:50:00', '2022-04-23 14:40:00')
(这里只有一个条目)可能的修复 :
last_timestamp = (df['utc_time'].max() +
pd.Timedelta('10 minutes')).timestamp()
resampling_factor
比你最初计算的要大一点。 df.resample
中的前两个数据帧并忽略只有一个或几个条目的第三个 选择在您的应用程序中最有意义的。
2. 看看我们现在拥有的
df.resample
docs,我们知道返回的标签是左边的 bin 边缘
In [67]: resamp = df.resample(f'{resampling_factor}S')
In [68]: itr = iter(resamp)
In [69]: next(itr)
Out[69]:
(Timestamp('2016-09-28 00:00:00', freq='58542600S'),
data_value
utc_time
2016-09-28 21:10:00 140.0
... ...
2018-08-06 13:40:00 230.0
[69889 rows x 1 columns])
In [70]: next(itr)
Out[70]:
(Timestamp('2018-08-06 13:50:00', freq='58542600S'),
data_value
utc_time
2018-08-06 13:50:00 230.0
... ...
2020-06-14 03:30:00 280.0
[97571 rows x 1 columns])
In [71]: next(itr)
Out[71]:
(Timestamp('2020-06-14 03:40:00', freq='58542600S'),
data_value
utc_time
2020-06-14 03:40:00 280.0
... ...
2020-06-15 00:50:00 280.0
[128 rows x 1 columns])
['2016-09-28 00:00:00', '2018-08-06 13:50:00')
['2018-08-06 13:50:00', '2020-06-14 03:40:00')
['2020-06-14 03:40:00', '2022-04-22 17:30:00')
(端点通过将 resampling_factor
添加到 bin 的开头来计算。)df['utc_time'].min
开始的( 2016-09-28 21:10:00
),但它是从那天开始的(如您所料)3. 固定起始仓左边缘
kind
参数可以是 'timestamp'
或 'period'
.如果你把它改成'period'
,您将拥有以下垃圾箱(带有 closed='left'
):['2016-09-28 21:10:00', '2018-08-07 11:00:00')
<-- 固定 ['2018-08-07 11:00:00', '2020-06-15 00:50:00')
['2020-06-15 00:50:00', '2022-04-23 14:40:00')
(使用“1”中给出的选项删除。)关于python - Pandas DataFrame 重新采样中出现意外数量的 bin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62937644/