第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

檢查一行中的日期是否早于下一行中的另一個日期

檢查一行中的日期是否早于下一行中的另一個日期

慕斯709654 2023-03-22 16:05:00
我在 Python 中有以下代碼:import pandas as pdimport numpy as npdate_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_rngdf['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')結(jié)果,我有以下數(shù)據(jù)框:  start_timestamp     end_timestamp2020-05-18 00:00:30 2020-05-18 00:54:072020-05-18 00:01:40 2020-05-18 03:50:392020-05-18 00:02:08 2020-05-18 02:39:412020-05-18 00:04:01 2020-05-18 00:47:252020-05-18 00:04:01 2020-05-18 02:26:502020-05-18 00:04:44 2020-05-18 02:17:53                .                .                .我應(yīng)該怎么做才能確保在我的數(shù)據(jù)集中每個end_timestamp都是在其下一行之前的日期時間start_timestamp?已實(shí)施的解決方案我基本上將數(shù)據(jù)集轉(zhuǎn)換為數(shù)組,將其按升序排列并將其轉(zhuǎn)換回?cái)?shù)據(jù)框。它可能不是最優(yōu)雅的解決方案,但它工作正常并為我打算使用的內(nèi)容生成了一致的數(shù)據(jù)。import pandas as pdimport numpy as npdate_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_rngdf['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')
查看完整描述

1 回答

?
揚(yáng)帆大魚

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超9個贊

編寫您的邏輯代碼,一切都很好

  1. freq='S'沒有任何意義,您將生成與開始日期和結(jié)束日期之間的秒數(shù)一樣多的行

  2. 在隨機(jī)化開始時間后,使用當(dāng)前行和下一行作為結(jié)束時間隨機(jī)函數(shù)的種子。這是作為列表理解嗎

  3. 在范圍的開始和結(jié)束處獲取 UTC 秒數(shù)時更聰明一些

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')


查看完整回答
反對 回復(fù) 2023-03-22
  • 1 回答
  • 0 關(guān)注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號