![](/img/trans.png)
[英]Datetime string needs to be converted to datetime object before matching on pandas column
[英]Transforming string containing a date before 1600 to datetime object in Pandas
我正在研究中世紀羊皮紙的數據集。 嘗試在 pandas DateTime object 中以這種格式 '1140 01 12' '%Y %m %d' 轉換日期。 不幸的是,pandas 僅處理 1660 以內的日期,分辨率為 ns。 我可以接受一天的決議,但我無法讓它發揮作用:
import pandas as pd
d = {'date': ['1140 01 12', '1140 02 16','1140 04 12','1200 10 27'], 'col2': [3, 4,5,6]}
pd.to_datetime(d['date'], format='%Y %m %d',origin = "julian", unit = "D")
我有ValueError: incompatible 'arg' type for given 'origin'='julian'
知道如何解決這個問題嗎?
您可以嘗試pd.Period
:
def conv(x):
year, day, month = map(int, x.split(" "))
return pd.Period(year=year, month=month, day=day, freq="D")
d = {"date": ["1140 01 12", "1140 02 16", "1140 04 12"], "col2": [3, 4, 5]}
df = pd.DataFrame(d)
df["date"] = df["date"].apply(conv)
print(df)
印刷:
date col2
0 1140-12-01 3
1 1147-03-18 4
2 1140-12-04 5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.