将包含1600之前日期的字符串转换为Pandas中的datetime对象



我正在处理一个中世纪羊皮纸的数据集。正在尝试在pandas DateTime对象中转换格式为"1140 01 12"%Y%m%d"的日期。不幸的是,熊猫只能以ns分辨率处理1660年左右的日期。我可以用一天的决心,但我不能让它发挥作用:

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

最新更新