我已嘗試使用此代碼將我的列之一 search_departure_date 從資料框 df 轉換為 datetimeformat 以獲得以下錯誤。
df
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 182005 entries, 0 to 182004
Data columns (total 19 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 date 182005 non-null datetime64[ns]
1 device_type 182005 non-null object
2 search_origin 182005 non-null object
3 search_destination 182005 non-null object
4 search_route 182005 non-null object
5 search_adult_count 157378 non-null float64
6 search_child_count 157378 non-null float64
7 search_cabin_class 157378 non-null object
8 search_type 182005 non-null object
9 search_departure_date 182005 non-null object
10 search_arrival_date 97386 non-null datetime64[ns]
df["search_departure_date"] = pd.to_datetime(df.loc[:, 'search_departure_date'], format='%Y-%m-%d')
OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1478-06-14 17:17:56
OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1479-03-23 17:17:56
所以我試圖用這個時間戳值過濾掉行
df.loc[df['search_departure_date'] != '1478-06-14 17:17:56']
df.loc[df['search_departure_date'] != '1479-03-23 17:17:56']
df.loc[df['search_departure_date'] != '1478-06-14 17:17:56']
我怎樣才能為多個時間戳做到這一點?我注意到它們都以 1478 或 1479 開頭,僅使用管道 (|) 運算子將它們連接在一起很麻煩。
uj5u.com熱心網友回復:
你可以試試errors='coerce'
df["search_departure_date"] = pd.to_datetime(df['search_departure_date'], errors='coerce')
如果要過濾掉行,可以使用str.match
m = df['search_departure_date'].str.match('147(8|9)')
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/487187.html
標籤:python-3.x 熊猫 数据框 约会时间
上一篇:轉換串列中年資料的天數資料
