将 Pandas 列转换为日期时间
我在 pandas DataFrame 中有一个字段以字符串格式导入。
它应该是一个日期时间变量。如何将其转换为日期时间列,然后根据日期进行过滤?
例子:
raw_data = pd.DataFrame({'Mycol': ['05SEP2014:00:00:00.000']})
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?
Example:
raw_data = pd.DataFrame({'Mycol': ['05SEP2014:00:00:00.000']})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用
to_datetime
函数,指定格式来匹配您的数据。Use the
to_datetime
function, specifying a format to match your data.如果您有多于一列需要转换,您可以执行以下操作:
If you have more than one column to be converted you can do the following:
编辑:建议使用
pd.to_datetime()
而不是这个,因为.apply()
通常速度较慢。您可以使用 DataFrame 方法
.apply( )
对 Mycol 中的值进行操作:edit: recommending to use
pd.to_datetime()
instead of this because.apply()
is generally slower.You can use the DataFrame method
.apply()
to operate on the values in Mycol:使用 pandas
to_datetime
函数将该列解析为 DateTime。此外,通过使用infer_datetime_format=True
,它将自动检测格式并将提到的列转换为 DateTime。Use the pandas
to_datetime
function to parse the column as DateTime. Also, by usinginfer_datetime_format=True
, it will automatically detect the format and convert the mentioned column to DateTime.节省时间:
Time Saver:
设置正确的
format=
比让 pandas 找出来要快得多1长话短说,从一开始就传递正确的
format=
,如 < a href="https://stackoverflow.com/a/26763793/19123103">chrisb 的帖子 比让 pandas 弄清楚格式要快得多,特别是当格式包含 时间 组件时。大于 10k 行的数据帧的运行时差异是巨大的(大约快 25 倍,所以我们说的是几分钟而不是几秒钟)。所有有效的格式选项均可在 https://strftime.org/ 中找到。errors='coerce'
很有用如果某些行的格式不正确或根本不是日期时间,
errors=
参数非常有用,这样您就可以可以转换有效行和句柄稍后包含无效值的行。静音
SettingWithCopyWarning
顺便说一句,如果您收到此警告,则意味着您的数据框可能是通过过滤另一个数据框创建的。启用写时复制就可以了。 (有关更多信息,请参阅这篇文章)。
1 用于生成 timeit 测试图的代码。
如果该列包含多种格式,请参阅将混合格式字符串列转换为日期时间 Dtype。
Setting the correct
format=
is much faster than letting pandas find out1Long story short, passing the correct
format=
from the beginning as in chrisb's post is much faster than letting pandas figure out the format, especially if the format contains time component. The runtime difference for dataframes greater than 10k rows is huge (~25 times faster, so we're talking like a couple minutes vs a few seconds). All valid format options can be found at https://strftime.org/.errors='coerce'
is usefulIf some rows are not in the correct format or not datetime at all,
errors=
parameter is very useful, so that you can convert the valid rows and handle the rows that contained invalid values later.To silence
SettingWithCopyWarning
On a side note, if you got this warning, then that means your dataframe was probably created by filtering another dataframe. Enable copy-on-write and you're good to go. (see this post for more about it).
1 Code used to produce the timeit test plot.
If the column contains multiple formats, see Convert a column of mixed format strings to a datetime Dtype.
就像我们将对象数据类型转换为 float 或 int 一样,使用 astype ()。
Just like we convert object data type to float or int, use astype().