Pandas,将日期时间格式 mm/dd/yyyy 转换为 dd/mm/yyyy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52869614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:05:35  来源:igfitidea点击:

Pandas, convert datetime format mm/dd/yyyy to dd/mm/yyyy

pythonpandasdatedatetime

提问by Amn Kh

The default format of csv is dd/mm/yyyy. When I convert it to datetime by df['Date']=pd.to_datetime(df['Date']), it change the format to mm//dd/yyyy.

csv 的默认格式为 dd/mm/yyyy。当我将其转换为 datetime by 时df['Date']=pd.to_datetime(df['Date']),它将格式更改为 mm//dd/yyyy。

Then, I used df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%d/%m/%Y') to convert to dd/mm/yyyy, But, they are in the string (object) format. However, I need to change them to datetimeformat. When I use again this (df['Date']=pd.to_datetime(df['Date'])), it gets back to the previous format. Need your help

然后,我曾经df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%d/%m/%Y') 转换为dd/mm/yyyy,但是,它们是字符串(对象)格式。但是,我需要将它们更改为datetime格式。当我再次使用这个 ( df['Date']=pd.to_datetime(df['Date'])) 时,它会恢复到以前的格式。需要你的帮助

采纳答案by rje

You can use the parse_datesand dayfirstarguments of pd.read_csv, see: the docs for read_csv()

您可以使用parse_dates和 的dayfirst参数pd.read_csv,请参阅:read_csv() 的文档

df = pd.read_csv('myfile.csv', parse_dates=['Date'], dayfirst=True)

This will read the Datecolumn as datetime values, correctly taking the first part of the date input as the day. Note that in general you will want your dates to be stored as datetime objects.

这会将Date列读取为日期时间值,正确地将日期输入的第一部分作为日期。请注意,通常您希望将日期存储为日期时间对象。

Then, if you need to output the dates as a string you can call dt.strftime():

然后,如果您需要将日期输出为字符串,您可以调用dt.strftime()

df['Date'].dt.strftime('%d/%m/%Y')

回答by jpp

When I use again this: df['Date'] = pd.to_datetime(df['Date']), it gets back to the previous format.

当我再次使用 this: 时df['Date'] = pd.to_datetime(df['Date']),它会恢复到以前的格式。

No, you cannot simultaneously have the string format of your choice and keep your series of type datetime. As remarked here:

,您不能同时拥有您选择的字符串格式并保留您的系列类型datetime。正如这里所说:

datetimeseries are stored internally as integers. Any human-readable date representation is just that, a representation, not the underlying integer. To access your custom formatting, you can use methods available in Pandas. You can even store such a text representation in a pd.Seriesvariable:

formatted_dates = df['datetime'].dt.strftime('%m/%d/%Y')

The dtypeof formatted_dateswill be object, which indicates that the elements of your series point to arbitrary Python times. In this case, those arbitrary types happen to be all strings.

Lastly, I strongly recommend you do notconvert a datetimeseries to strings until the very last step in your workflow. This is because as soon as you do so, you will no longer be able to use efficient, vectorised operations on such a series.

datetime系列在内部存储为整数。任何人类可读的日期表示都只是一个表示,而不是底层整数。要访问自定义格式,您可以使用 Pandas 中可用的方法。您甚至可以将这样的文本表示存储在pd.Series变量中:

formatted_dates = df['datetime'].dt.strftime('%m/%d/%Y')

dtypeformatted_dates将是object,这表明你的系列点到任意Python倍的元素。在这种情况下,那些任意类型恰好都是字符串。

最后,我强烈建议您在工作流程的最后一步之前不要datetime系列转换为字符串。这是因为一旦您这样做,您将不再能够对这样的系列使用高效的矢量化操作。