使用這個資料集:
import numpy as np
import pandas as pd
# Create a reproducible, static dataframe.
# 1 minute SPY data. Skip to the bottom...
df = pd.DataFrame([
{
"time": "2021-10-26 9:30",
"open": "457.2",
"high": "457.29",
"low": "456.78",
"close": "456.9383",
"volume": "594142"
},
{
"time": "2021-10-26 9:31",
"open": "456.94",
"high": "457.07",
"low": "456.8",
"close": "456.995",
"volume": "194061"
},
{
"time": "2021-10-26 9:32",
"open": "456.99",
"high": "457.22",
"low": "456.84",
"close": "457.21",
"volume": "186114"
},
{
"time": "2021-10-26 9:33",
"open": "457.22",
"high": "457.45",
"low": "457.2011",
"close": "457.308",
"volume": "294158"
},
{
"time": "2021-10-26 9:34",
"open": "457.31",
"high": "457.4",
"low": "457.25",
"close": "457.32",
"volume": "172574"
},
{
"time": "2021-10-26 9:35",
"open": "457.31",
"high": "457.48",
"low": "457.18",
"close": "457.44",
"volume": "396668"
},
{
"time": "2021-10-26 9:36",
"open": "457.48",
"high": "457.6511",
"low": "457.44",
"close": "457.57",
"volume": "186777"
}
])
...我想計算一個布爾列,表示True“時間”列中的 TIME(日期不重要)是否在開始和結束時間之內,否則False。到目前為止我所擁有的是:
from datetime import datetime
start_time = datetime.strptime("09" "31", "%H%M").time()
end_time = datetime.strptime("09" "33", "%H%M").time()
# Convert datetime column to datetime objects
df['time'] = pd.to_datetime(df['time'])
df['isWithinTimeframe'] = np.where(df['time'].time() >= start_time and df['time'].time() < end_time, True, False)
我目前收到錯誤:
AttributeError: 'Series' object has no attribute 'time'
我閱讀了其他一些您需要.dt在df['time']and之間呼叫的解決方案.time(),所以我也嘗試過,但得到:
TypeError: 'Series' object is not callable
想法?
uj5u.com熱心網友回復:
一些必要的更正將使您獲得布爾標志。
如評論中所述,您必須使用.dt來訪問日期時間方法和屬性(類似于如何.str提供對字串方法和屬性的訪問)
而不是and使用按位與運算子&,并且為了避免由于運算子優先級而導致的錯誤,必須將>=&<比較放在括號中。
df['isWithinTimeframe'] = (df.time.dt.time >= start_time) & (df.time.dt.time < end_time)
df 現在看起來像:
| 時間 | 打開 | 高的 | 低的 | 關閉 | 體積 | 在時間范圍內 | |
|---|---|---|---|---|---|---|---|
| 0 | 2021-10-26 09:30:00 | 457.2 | 457.29 | 456.78 | 456.938 | 594142 | 錯誤的 |
| 1 | 2021-10-26 09:31:00 | 456.94 | 457.07 | 456.8 | 456.995 | 194061 | 真的 |
| 2 | 2021-10-26 09:32:00 | 456.99 | 457.22 | 456.84 | 457.21 | 186114 | 真的 |
| 3 | 2021-10-26 09:33:00 | 457.22 | 457.45 | 457.201 | 457.308 | 294158 | 錯誤的 |
| 4 | 2021-10-26 09:34:00 | 457.31 | 457.4 | 457.25 | 457.32 | 172574 | 錯誤的 |
| 5 | 2021-10-26 09:35:00 | 457.31 | 457.48 | 457.18 | 457.44 | 396668 | 錯誤的 |
| 6 | 2021-10-26 09:36:00 | 457.48 | 457.651 | 457.44 | 457.57 | 186777 | 錯誤的 |
uj5u.com熱心網友回復:
將列時間強制轉換為日期時間并將其設定為索引。這允許您使用 between_time 子句來選擇適合您的條件的內容。因為這會回傳一個 subdf,所以檢查 subdf 中的時間列是否在主 df 中。下面的代碼
df['isWithinTimeframe']=df['time'].isin(df.set_index(pd.to_datetime(df['time'])).between_time(start_time,end_time)['time'])
time open high low close volume \
0 2021-10-26 9:30 457.2 457.29 456.78 456.9383 594142
1 2021-10-26 9:31 456.94 457.07 456.8 456.995 194061
2 2021-10-26 9:32 456.99 457.22 456.84 457.21 186114
3 2021-10-26 9:33 457.22 457.45 457.2011 457.308 294158
4 2021-10-26 9:34 457.31 457.4 457.25 457.32 172574
5 2021-10-26 9:35 457.31 457.48 457.18 457.44 396668
6 2021-10-26 9:36 457.48 457.6511 457.44 457.57 186777
isWithinTimeframe
0 False
1 True
2 True
3 True
4 False
5 False
6 False
print(df.where(df['life_day
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/370524.html
