【发布时间】:2020-11-17 12:18:35
【问题描述】:
我是 pandas 的新手,我正在尝试将日期格式为“%d %B”(1 月 1 日、1 月 2 日 ....)的字符串列转换为日期时间对象,列的类型是<class 'pandas.core.series.Series'> 。
如果我在 to_datetime 方法中传入这个系列,比如
print(pd.to_datetime(data_file['Date'], format='%d %B', errors="coerce"))
所有条目都返回NaT,它应该返回日期时间对象
我检查了文档,它说它接受一个 Series 对象。
有什么办法解决这个问题?
编辑 1: 这是我正在使用的数据的头部:
Date Daily Confirmed
0 30 January 1
1 31 January 0
2 01 February 0
3 02 February 1
4 03 February 1
编辑2:这里是数据的信息
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 179 entries, 0 to 178
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Date 179 non-null object
1 Daily Confirmed 179 non-null int64
dtypes: int64(1), object(1)
memory usage: 2.2+ KB
【问题讨论】:
-
可能是
format='%d %B'不匹配 -
匹配,如果 id 为
print(pd.to_datetime(["01 January"], format='%d %B', errors="coerce")),则返回一个日期时间对象 -
好的,你能分享一些示例数据吗?
-
我已将数据添加到帖子中(所有日期也是字符串)
-
它不能转换为没有年份的日期时间。
标签: python pandas dataframe time-series