【问题标题】:Check if a date on a row is earlier than another date on the next row检查一行上的日期是否早于下一行的另一个日期
【发布时间】:2020-07-22 03:27:51
【问题描述】:

我在 Python 中有以下代码:

import pandas as pd
import numpy as np
date_rng = pd.date_range(start='5/18/2019', end='7/22/2020', freq='S')

df = pd.DataFrame(date_rng, columns=['start_timestamp'])
df['end_timestamp'] = date_rng
df['start_timestamp'] = np.random.randint(1589760000,1595376000,size=(len(date_rng)))
df['end_timestamp'] = np.random.randint(1589760000,1595376000,size=(len(date_rng)))
df = df[(df.end_timestamp/df.start_timestamp<=1.000009)&(df.end_timestamp/df.start_timestamp>=1.000001)]
df = df.sort_values(by=['start_timestamp','end_timestamp'])
df['start_timestamp'] = pd.to_datetime(df['start_timestamp'],unit='s')
df['end_timestamp'] = pd.to_datetime(df['end_timestamp'],unit='s')

因此,我有以下数据框:

  start_timestamp     end_timestamp
2020-05-18 00:00:30 2020-05-18 00:54:07
2020-05-18 00:01:40 2020-05-18 03:50:39
2020-05-18 00:02:08 2020-05-18 02:39:41
2020-05-18 00:04:01 2020-05-18 00:47:25
2020-05-18 00:04:01 2020-05-18 02:26:50
2020-05-18 00:04:44 2020-05-18 02:17:53

                .
                .
                .

我应该怎么做才能确保在我的数据集中每个end_timestamp 都是在其下一行的start_timestamp 之前的日期时间?

已实施的解决方案

我基本上将数据集转换为数组,将其按升序排列,然后将其转换回数据框。它可能不是最优雅的解决方案,但它可以正常工作并为我打算使用的内容生成一致的数据。

import pandas as pd
import numpy as np
date_rng = pd.date_range(start='7/22/2019', end='7/22/2020', freq='S')

df = pd.DataFrame(date_rng, columns=['start_timestamp'])
df['end_timestamp'] = date_rng
df['start_timestamp'] = np.random.randint(1563753600,1595376000,size=(len(date_rng)))
df['end_timestamp'] = np.random.randint(1563753600,1595376000,size=(len(date_rng)))
df = df[(df.end_timestamp/df.start_timestamp<=1.0000009)&(df.end_timestamp/df.start_timestamp>=1.0000001)]
df = df.to_numpy()
df = df.reshape(df.shape[0]*2,1)
df = np.sort(df,axis=0)
df = df.reshape(int(df.shape[0]/2),2)
df = pd.DataFrame(df,columns=['start_timestamp','end_timestamp'])
df['start_timestamp'] = pd.to_datetime(df['start_timestamp'],unit='s')
df['end_timestamp'] = pd.to_datetime(df['end_timestamp'],unit='s')

【问题讨论】:

    标签: python pandas dataframe timestamp


    【解决方案1】:

    编写你的逻辑,一切都很好

    1. freq='S' 毫无意义,您将生成与开始日期和结束日期之间的秒数一样多的行
    2. 在开始时间随机化后,使用当前行和下一行作为结束时间的随机函数的种子。这样做是为了理解列表
    3. 在获取范围开始和结束时的 UTC 秒数方面更聪明了
    
    import pandas as pd
    import numpy as np
    from datetime import datetime
    # date_rng = pd.date_range(start='5/18/2019', end='7/22/2020', freq='S')
    date_rng = pd.date_range(start='5/18/2019', end='5/19/2019', freq='min')
    
    sec = [(date_rng.min() - datetime(1970, 1, 1)).total_seconds(),
           (date_rng.max() - datetime(1970, 1, 1)).total_seconds() ]
    df = pd.DataFrame(date_rng, columns=['start_timestamp'])
    df['start_timestamp'] = np.random.randint(sec[0],sec[1],size=(len(date_rng)))
    df = df.sort_values(by="start_timestamp")
    l = df["start_timestamp"].tolist()  # get randomised start times
    l[-1] = sec[1] # set last time to end of range
    # randomise end time between two start times
    df['end_timestamp'] = [np.random.randint(l[i], l[i+1]) if i<len(l)-1  and l[i]<l[i+1] else l[i] for i, s in enumerate(l)]
    df['start_timestamp'] = pd.to_datetime(df['start_timestamp'],unit='s')
    df['end_timestamp'] = pd.to_datetime(df['end_timestamp'],unit='s')
    
    
    

    【讨论】:

    • 我将值以秒为单位,因为在将使用此数据的应用程序中,我需要使用这种精细程度。总之感谢。我会确定你的答案是否被接受。
    • 自从发布这个答案后,我意识到有一些更基本的东西——随机整数的排序列表几乎等同于整数序列。您还注意到 startdate 和 enddate 不是独立的随机变量。所以这真的回到了数学/统计
    猜你喜欢
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2015-10-26
    相关资源
    最近更新 更多