【问题标题】:Comparing pandas timestamps of different resolutions比较不同分辨率的 pandas 时间戳
【发布时间】:2019-03-21 01:07:46
【问题描述】:

我有两个时间序列数据框(约 45k 行与 5 行)。一个具有低至毫秒的时间戳,另一个低至秒。我想在较大的数据框中创建一个新列,以便: a)一个值被附加到较大数据帧中的行,其时间戳最接近(以秒为单位)较小数据帧中的时间戳 b) 任何其他时间戳的 NaN。

larger df = 
            timestamp           price
0       2018-04-24 06:01:02.600 1
1       2018-04-24 06:01:02.600 1
2       2018-04-24 06:01:02.600 2
3       2018-04-24 06:01:02.600 4
4       2018-04-24 06:01:02.775 2
5       2018-04-24 06:01:02.825 3
6       2018-04-24 06:01:03.050 5
7       2018-04-24 06:01:03.125 6
8       2018-04-24 06:01:03.275 7
9       2018-04-24 06:01:03.300 4
10      2018-04-24 06:01:03.300 3
11      2018-04-24 06:01:03.950 5
12      2018-04-24 06:01:04.050 5


smaller df = 
   timestamp           price
0   24/04/2018 06:01:02 2
1   24/04/2018 12:33:37 4   
2   24/04/2018 14:29:34 5   
3   24/04/2018 15:02:50 6   
4   24/04/2018 15:20:04 7   

desired df =

            timestamp       price  newCol
0       2018-04-24 06:01:02.600 1   aValue
1       2018-04-24 06:01:02.600 1   NaN
2       2018-04-24 06:01:02.600 2   NaN
3       2018-04-24 06:01:02.600 4   NaN
4       2018-04-24 06:01:02.775 2   NaN
5       2018-04-24 06:01:02.825 3   NaN
6       2018-04-24 06:01:03.050 5   NaN
7       2018-04-24 06:01:03.125 6   NaN
8       2018-04-24 06:01:03.275 7   NaN
9       2018-04-24 06:01:03.300 4   NaN
10      2018-04-24 06:01:03.300 3   NaN
11      2018-04-24 06:01:03.950 5   NaN
12      2018-04-24 06:01:04.050 5   NaN

我们将非常感谢您的帮助。一般来说,我对编程还是太陌生,无法轻松解决这个问题。

非常感谢

【问题讨论】:

    标签: python pandas dataframe time-series


    【解决方案1】:

    reindex

    为了只使用一次值,我必须从较小的数据帧中跟踪时间戳。所以我在reindex'nearest' 时包含了这些值。然后我在掩码中使用duplicated

    df_small_new = df_small.set_index('timestamp', drop=False)
    df_small_new = df_small_new.reindex(df_large.timestamp, method='nearest')
    
    
    df_large.assign(
        newcol=df_small_new.price.mask(df_small_new.timestamp.duplicated()).values)
    
                     timestamp  price  newcol
    0  2018-04-24 06:01:02.600      1     2.0
    1  2018-04-24 06:01:02.600      1     NaN
    2  2018-04-24 06:01:02.600      2     NaN
    3  2018-04-24 06:01:02.600      4     NaN
    4  2018-04-24 06:01:02.775      2     NaN
    5  2018-04-24 06:01:02.825      3     NaN
    6  2018-04-24 06:01:03.050      5     NaN
    7  2018-04-24 06:01:03.125      6     NaN
    8  2018-04-24 06:01:03.275      7     NaN
    9  2018-04-24 06:01:03.300      4     NaN
    10 2018-04-24 06:01:03.300      3     NaN
    11 2018-04-24 06:01:03.950      5     NaN
    12 2018-04-24 06:01:04.050      5     NaN
    

    pandas.merge_asof

    • 重命名小数据框中的'price'
    • 确保将direction 设置为'nearest'
    • 这几乎回答了问题

    pd.merge_asof(
        df_large,
        df_small.rename(columns={'price': 'newcol'}),
        on='timestamp', direction='nearest'
    )
    
                     timestamp  price  newcol
    0  2018-04-24 06:01:02.600      1       2
    1  2018-04-24 06:01:02.600      1       2
    2  2018-04-24 06:01:02.600      2       2
    3  2018-04-24 06:01:02.600      4       2
    4  2018-04-24 06:01:02.775      2       2
    5  2018-04-24 06:01:02.825      3       2
    6  2018-04-24 06:01:03.050      5       2
    7  2018-04-24 06:01:03.125      6       2
    8  2018-04-24 06:01:03.275      7       2
    9  2018-04-24 06:01:03.300      4       2
    10 2018-04-24 06:01:03.300      3       2
    11 2018-04-24 06:01:03.950      5       2
    12 2018-04-24 06:01:04.050      5       2
    

    【讨论】:

    • 嗨 piRSquared,感谢您的快速响应。我将如何确保我在 newCol 中没有重复值?理想情况下,newCol 只会在第一行显示一个值,而在其余行显示 NaN。当秒时间戳相同时,我什至可以显示相同的值(在本例中为“2”)
    • 哦,您只想使用该值一次?我没看到。给我一点时间
    猜你喜欢
    • 2015-01-17
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 2023-03-16
    • 2012-12-21
    相关资源
    最近更新 更多