简体   繁体   English

熊猫:无法将日期时间YYYY-mm-dd转换为dd-mm-YYYY

[英]Pandas: can't convert datetime YYYY-mm-dd to dd-mm-YYYY

I have a pandas dataframe with column with dates (datetime64[ns] format) The dates are listed as yyyy-mm-dd (like 2018-09-12). 我有一个带有日期列(日期时间64 [ns]格式)的pandas数据框,日期以yyyy-mm-dd列出(如2018-09-12)。 I want to convert it to dd-mm-yyyy but what i try doesn't seem to works: 我想将其转换为dd-mm-yyyy,但我尝试执行的操作似乎无效:

pd.to_datetime(df['date'], format='%d-%m-%Y')
0    2018-09-12
1    2018-09-12

Edit: add example df 编辑:添加示例df

df.head(3)
        date      nr
0   2018-09-12   144
1   2018-09-12    37
2   2018-09-12    28

df.info()
date   datetime64[ns]
nr     int

The 'date' doesn't contain any Nan's or Nat's “日期”不包含任何Nan或Nat

You need to convert the date parsed with to_datetime to a string in the desired representation. 您需要将使用to_datetime解析的日期转换为所需表示形式的字符串。 The display format you see right now is the default one used for Datetime objects: 您现在看到的显示格式是用于Datetime对象的默认格式:

In [10]: frame = {'date': ['2018-09-12', '2016-10-02']}

In [11]: pd.to_datetime(frame['date']).strftime('%d-%m-%Y')
Out[11]: Index(['12-09-2018', '02-10-2016'], dtype='object')

The dates are listed as yyyy-mm-dd (like 2018-09-12). 日期以yyyy-mm-dd列出(例如2018-09-12)。

Yes, but this is not how they are stored. 是的,但这不是它们的存储方式。 What you are seeing is a specific string representation of datetime objects. 您将看到datetime对象的特定字符串表示形式 Internally, datetime values are stored as integers . 在内部, datetime存储为integers

I want to convert it to dd-mm-yyyy but what i try doesn't seem to works 我想将其转换为dd-mm-yyyy,但我尝试执行的操作似乎无效

Correct, because pd.to_datetime converts to a datetime object. 正确,因为pd.to_datetime转换为datetime对象。 You need to instead convert your series to a series of strings, which will have object dtype: 您需要将系列转换为一系列字符串,这些字符串将具有object dtype:

df['date'] = df['date'].dt.strftime('%d-%m-%Y')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas 日期时间:无法将 dd-mm-YYYY 格式的对象转换为正确格式的日期时间 - Pandas datetime: can't convert object in dd-mm-YYYY format to datetime in correct format Pandas,将日期时间格式 mm/dd/yyyy 转换为 dd/mm/yyyy - Pandas, convert datetime format mm/dd/yyyy to dd/mm/yyyy 为什么 Pandas to_datetime() function 在同一数据系列 (YYYY-DD-MM) 和 (YYYY-MM-DD) 中以两种不同格式返回 (DD-MM-YYYY) 日期时间? - Why is Pandas to_datetime() function returning (DD-MM-YYYY) datetime in two different formats in the same data series (YYYY-DD-MM) and (YYYY-MM-DD)? 将熊猫日期时间列 yyyy-mm-dd 转换为 YYYYMMDD - convert pandas datetime column yyyy-mm-dd to YYYYMMDD 如何在 Python 中将 mysql 日期时间 yyyy-mm-dd 格式化为 dd-mm-yyyy? - How to format a mysql datetime yyyy-mm-dd to dd-mm-yyyy in Python? 如何将不同格式的字符串(YYYY-MM-DD,DD-MM-YYYY)转换为熊猫中一种格式的日期对象? - How to convert string of different format(YYYY-MM-DD, DD-MM-YYYY) to date object of one format in pandas? 在python中将yyyy-dd-mm转换为yyyy-mm-dd - convert yyyy-dd-mm to yyyy-mm-dd in python 将日期时间打印为DD-MM-YYYY - Print out datetime as DD-MM-YYYY 如何将混合日期格式mm-dd-yyyy和yyyy-mm-dd转换为熊猫中的日期 - how to convert mix date formats mm-dd-yyyy and yyyy-mm-dd to date in pandas 匹配日期时间 YYYY-MM-DD object 在 pandas dataframe - Match datetime YYYY-MM-DD object in pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM