python - Pandas DataFrame 重新采样中出现意外数量的 bin

标签 python python-3.x pandas dataframe resampling

问题
我需要将 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])

最佳答案

概括

  • 问题 1 和修复:您形成垃圾箱的方式将使 一个额外的垃圾箱 因为使用 df.resample() 创建的垃圾箱将仅在一端(左侧或右侧)关闭。使用“1.”中列出的选项之一修复此问题。
  • 问题 2 和修复:第一个 bin 左边缘位于当天开始时 ('2016-09-28 00:00:00')(参见“2.”)。您可以使用 kind='period' 修复它作为 resample() 的参数. (见“3”)

  • 1. 看一眼输入数据(以及我们需要什么样的垃圾箱)
    输入数据来自 2016-09-28 21:10:002020-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 的开头来计算。)

  • 我们看到第一个 bin 不是从 df['utc_time'].min 开始的( 2016-09-28 21:10:00 ),但它是从那天开始的(如您所料)
  • 由于第一个 bin 在预期之前开始,我们有两个 bin 之外的数据,在第三个 bin 中。

  • 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/

    相关文章:

    python - 霍夫曼编码问题

    python - Google Calendar API 缺少日历摘要

    python - Python 3.5 中的包导入失败

    python - 如何就地剥离 pandas 数据帧中的字符串的一部分,以将结果理解为带时区的时间戳?

    python - 无法在 pandas NamedAgg 函数中使用 lambda 函数

    python - 在python中合并具有不同长度和列的数据帧列表

    python - 将 View 链接到 Django 链接

    Python理解递归程序

    python - 使用 2captcha api 解决 Cloudflare Turnstile Captcha 问题

    python-3.x - 如何更改 sklearn.metrics 包中的 plot_confusion_matrix 默认图形大小