我有一个由 15 分钟间隔组成的 DateTimeIndex。
我也有以两种方式编写的相同函数,我想应用于整个数据帧。该函数的重点是获取特定日期是否为工作日。
他们来了:
def weekend(datum):
if (datum.weekday() == 5) or (datum.weekday() == 6):
return "Weekend"
else:
return "Working day"
# written with being fed the DateTimeIndex in mind
def weekendfromnumber(number):
if (number == 5) or (number == 6):
return "Weekend"
else:
return "Working day"
# written with being fed the integer of the intermediate columng weekday in mind
Run Code Online (Sandbox Code Playgroud)
我想通过直接使用 DateTimeIndex 来应用第一个函数,如下所示:
df15['Type of day'] = df15.index.apply(weekend)
Run Code Online (Sandbox Code Playgroud)
但我收到错误:
AttributeError: 'DatetimeIndex' object has no attribute 'apply'
Run Code Online (Sandbox Code Playgroud)
如果我使用第二个函数:
df15['Type of …Run Code Online (Sandbox Code Playgroud) 我有以下数据框。它是 OHLC 一分钟数据。显然我需要 T 列成为和索引才能使用时间序列功能
CHLOTV
13712 6873.0 6873.0 6873.0 6873.0 2018-01-13T17:17:00 799.448421
13713 6878.0 6878.0 6875.0 6875.0 2018-01-13T17:18:00 1707.578666
13714 6880.0 6880.0 6825.0 6825.0 2018-01-13T17:21:00 481.245707
13715 6876.0 6876.0 6876.0 6876.0 2018-01-13T17:22:00 839.177283
13716 6870.0 6878.0 6830.0 6878.0 2018-01-13T17:23:00 4336.830277
Run Code Online (Sandbox Code Playgroud)
我用了:
df['T'] = pd.to_datetime(df['T'])
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好!T 列现在被识别为日期
查看:
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 13717 entries, 1970-01-01 00:00:00 to 1970-01-01 00:00:00.000013716
Data columns (total 7 columns):
BV 13717 non-null float64
C 13717 non-null float64
H 13717 non-null float64
L 13717 non-null float64
O …Run Code Online (Sandbox Code Playgroud)