Python 将 Pandas 列转换为日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26763344/
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-08-19 00:59:17  来源:igfitidea点击:

Convert Pandas Column to DateTime

pythondatetimepandas

提问by Chris

I have one field in a pandas DataFrame that was imported as string format. It should be a datetime variable. How do I convert it to a datetime column and then filter based on date.

我在以字符串格式导入的 Pandas DataFrame 中有一个字段。它应该是一个日期时间变量。如何将其转换为日期时间列,然后根据日期进行过滤。

Example:

例子:

  • DataFrame Name: raw_data
  • Column Name: Mycol
  • Value Format in Column: '05SEP2014:00:00:00.000'
  • 数据帧名称:raw_data
  • 列名:Mycol
  • 列中的值格式:'05SEP2014:00:00:00.000'

采纳答案by chrisb

Use the to_datetimefunction, specifying a formatto match your data.

使用该to_datetime函数,指定一种格式以匹配您的数据。

raw_data['Mycol'] =  pd.to_datetime(raw_data['Mycol'], format='%d%b%Y:%H:%M:%S.%f')

回答by mechanical_meat

You can use the DataFrame method .apply()to operate on the values in Mycol:

您可以使用 DataFrame 方法.apply()对Mycol 中的值进行操作:

>>> df = pd.DataFrame(['05SEP2014:00:00:00.000'],columns=['Mycol'])
>>> df
                    Mycol
0  05SEP2014:00:00:00.000
>>> import datetime as dt
>>> df['Mycol'] = df['Mycol'].apply(lambda x: 
                                    dt.datetime.strptime(x,'%d%b%Y:%H:%M:%S.%f'))
>>> df
       Mycol
0 2014-09-05

回答by Darth BEHFANS

raw_data['Mycol'] =  pd.to_datetime(raw_data['Mycol'], format='%d%b%Y:%H:%M:%S.%f')

works, however it results in a Python warning of A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = valueinstead

工作,但是它会导致 Python 警告 A value is试图设置在来自 DataFrame 的切片的副本上。尝试.loc[row_indexer,col_indexer] = value改用

I would guess this is due to some chaining indexing.

我猜这是由于一些链接索引。

回答by Vlad Bezden

If you have more than one column to be converted you can do the following:

如果要转换的列不止一列,则可以执行以下操作:

df[["col1", "col2", "col3"]] = df[["col1", "col2", "col3"]].apply(pd.to_datetime)

回答by Prateek Sharma

Use the pandas to_datetimefunction to parse the column as DateTime. Also, by using infer_datetime_format=True, it will automatically detect the format and convert the mentioned column to DateTime.

使用 pandasto_datetime函数将列解析为 DateTime。此外,通过使用infer_datetime_format=True,它会自动检测格式并将提到的列转换为日期时间。

import pandas as pd
raw_data['Mycol'] =  pd.to_datetime(raw_data['Mycol'], infer_datetime_format=True)