亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

優雅的熊貓使用 date_range 和各種可能的頻率設置進行預填充

優雅的熊貓使用 date_range 和各種可能的頻率設置進行預填充

胡說叔叔 2021-12-08 16:03:34
我正在嘗試預填充類似于以下內容的數據框:在示例中,我隨機刪除了一些行以突出挑戰。我正在嘗試*優雅地計算 dti 值。第一行中的 dti 值將為 0(即使第一行按照腳本被刪除)但由于 dti 序列中出現間隙需要跳過缺失的行。一種合乎邏輯的方法是將 dt/delta 相除以創建一個唯一的整數來表示桶,但我嘗試過的任何東西都感覺不到或看起來很優雅。一些代碼來幫助模擬問題:from datetime import datetime, timedeltaimport pandas as pdimport numpy as npstart = datetime.now()nin = 24delta='4H'df = pd.date_range( start, periods=nin, freq=deltadf, name ='dt') # remove some random data pointsfrac_points = 8/24                  # Fraction of points to retainr = np.random.rand(nin)df = df[r <= frac_points]           # reduce the number of pointsdf = df.to_frame(index=False)       # reindexdf['dti'] = ...先感謝您,
查看完整描述

1 回答

?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

一種解決方案是將每行之間的時間差除以 timedelta:


from datetime import datetime, timedelta

import pandas as pd

import numpy as np


start = datetime.now()

nin = 24

delta='4H'


df = pd.date_range(start, periods=nin, freq=delta, name='dt')


# Round to nearest ten minutes for better readability

df = df.round('10min')


# Ensure reproducibility

np.random.seed(1)


# remove some random data points

frac_points = 8/24                  # Fraction of points to retain

r = np.random.rand(nin)

df = df[r <= frac_points]           # reduce the number of points

df = df.to_frame(index=False)       # reindex


df['dti'] = df['dt'].diff() / pd.to_timedelta(delta)

df['dti'] = df['dti'].fillna(0).cumsum().astype(int)

df


                   dt  dti

0 2019-03-17 18:10:00    0

1 2019-03-17 22:10:00    1

2 2019-03-18 02:10:00    2

3 2019-03-18 06:10:00    3

4 2019-03-18 10:10:00    4

5 2019-03-19 10:10:00   10

6 2019-03-19 18:10:00   12

7 2019-03-20 10:10:00   16

8 2019-03-20 14:10:00   17

9 2019-03-21 02:10:00   20


查看完整回答
反對 回復 2021-12-08
  • 1 回答
  • 0 關注
  • 229 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號