如何在DatetimeIndex的特定时间插入值

时间:2018-12-21 16:18:48

标签: python pandas datetime dataframe

我下面有一个可复制的代码:

import pandas as pd
import datetime

foo = pd.read_csv("http://m.uploadedit.com/bbtc/1545406250692.txt", header=None, names=["Stock","Date","Time", "Open", "High", "Low", "Close", "Volume", "OI"], dtype={"Stock":"category"}, parse_dates= [['Date', 'Time']], index_col="Date_Time")
foo.sort_index(inplace=True)
bar = foo.between_time('09:00:00', '15:30:00') #Dropping post and pre market data i.e. from index 15:31 - 16:35

#resampling the data by 120 Minutes (2 hours)
twohour = bar.loc["2018-11-22 09:08:00":].resample('120Min',closed = 'right',label = 'left', base=75).agg({'Open': 'first', 'High': 'max', 'Low': 'min','Close': 'last'}).dropna()
twohour.head(7)

Out[]:
                    Close   High    Open    Low
Date_Time               
2018-11-22 07:15:00 321.3   321.30  321.30  321.30
2018-11-22 09:15:00 324.5   326.90  320.10  320.00
2018-11-22 11:15:00 323.2   324.85  324.60  322.20
2018-11-22 13:15:00 319.9   324.35  323.20  319.50
2018-11-22 15:15:00 320.0   320.35  319.85  319.15
2018-11-26 07:15:00 324.90  324.90  324.90  324.90
2018-11-26 09:15:00 311.35  324.40  323.10  309.60

我希望将时间为Open的索引中09:15:00列中的每个值替换为时间为Close的索引中的07:15:00列中的值。

简而言之,我需要以下输出:

Out[]:
                    Close   High    Open    Low
Date_Time               
2018-11-22 07:15:00 321.3   321.30  321.30  321.30
2018-11-22 09:15:00 324.5   326.90  321.30  320.00
2018-11-22 11:15:00 323.2   324.85  324.60  322.20
2018-11-22 13:15:00 319.9   324.35  323.20  319.50
2018-11-22 15:15:00 320.0   320.35  319.85  319.15
2018-11-26 07:15:00 324.90  324.90  324.90  324.90
2018-11-26 09:15:00 311.35  324.40  324.90  309.60

我尝试通过将.loc转换为字典,然后替换值来使用DateTimeindex。但是字典没有排序,因此需要对dict进行排序,并且代码变得越来越难看。 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:4)

您可以使用loc选择所需的行,并将“打开”列设置为close.shift

import datetime 

df.loc[df.index.time == datetime.time(9, 15), 'Open'] = df['Close'].shift(1)


                    Close   High    Open    Low
Date_Time               
2018-11-22 07:15:00 321.30  321.30  321.30  321.30
2018-11-22 09:15:00 324.50  326.90  321.30  320.00
2018-11-22 11:15:00 323.20  324.85  324.60  322.20
2018-11-22 13:15:00 319.90  324.35  323.20  319.50
2018-11-22 15:15:00 320.00  320.35  319.85  319.15
2018-11-26 07:15:00 324.90  324.90  324.90  324.90
2018-11-26 09:15:00 311.35  324.40  324.90  309.60

编辑:比较时间

import time
start = time.clock()
df.loc[df.index.time == datetime.time(9, 15), 'Open'] = df['Close'].shift(1)
print (time.clock() - start)

0.006845999999999464


start = time.clock()
mask_bool = (df.index - df.index.normalize()) == '09:15:00'
df['Open'] = df['Open'].mask(mask_bool, df['Close'].shift(1))
print (time.clock() - start)

0.009392999999999319

答案 1 :(得分:2)

您可以在比较之前将索引转换为timdelta 字符串:

# timedelta option, vectorised & efficient
mask_bool = (df.index - df.index.normalize()) == '09:15:00'

# string alternative, inefficient
mask_bool = df.index.strftime('%H:%M') == '09:15'

然后通过locmask进行分配:

# Option 1: assign conditionally via loc
df.loc[mask_bool, 'Open'] = df['Close'].shift(1)

# Option 2: mask with pd.Series.mask
df['Open'] = df['Open'].mask(mask_bool, df['Close'].shift(1))

结果:

print(df)

                      Close    High    Open     Low
Date_Time                                          
2018-11-22 07:15:00  321.30  321.30  321.30  321.30
2018-11-22 09:15:00  324.50  326.90  321.30  320.00
2018-11-22 11:15:00  323.20  324.85  324.60  322.20
2018-11-22 13:15:00  319.90  324.35  323.20  319.50
2018-11-22 15:15:00  320.00  320.35  319.85  319.15
2018-11-26 07:15:00  324.90  324.90  324.90  324.90
2018-11-26 09:15:00  311.35  324.40  324.90  309.60

性能基准测试

对于较大的数据帧,timedelta矢量化版本应该是有效的,但是请注意,这将取决于系统和设置:

# Python 3.6.5, Pandas 0.23, NumPy 1.14.3

import pandas as pd
from datetime import time

df = pd.DataFrame.from_dict({'Date_Time': ['2018-11-22 07:15:00', '2018-11-22 09:15:00',
                                           '2018-11-22 11:15:00', '2018-11-22 13:15:00',
                                           '2018-11-22 15:15:00', '2018-11-26 07:15:00',
                                           '2018-11-26 09:15:00'],
                             'Close': [321.3, 324.5, 323.2, 319.9, 320.0, 324.9, 311.35],
                             'High': [321.3, 326.9, 324.85, 324.35, 320.35, 324.9, 324.4],
                             'Open': [321.3, 321.3, 324.6, 323.2, 319.85, 324.9, 324.9],
                             'Low': [321.3, 320.0, 322.2, 319.5, 319.15, 324.9, 309.6]})

df['Date_Time'] = pd.to_datetime(df['Date_Time'])
df = df.set_index('Date_Time')

df = pd.concat([df]*10**4)

%timeit (df.index - df.index.normalize()) == '09:15:00'  # 8.67 ms
%timeit df.index.strftime('%H:%M') == '09:15'            # 651 ms
%timeit df.index.time == time(9, 15)                     # 28.3 ms