简体   繁体   English

从数据框列中获取正确的日期时间对象,其中包含带有日期和时间的随机字符串

[英]Get correct datetime object from dataframe column with random string present with date and time

I have dataframe like this:我有这样的数据框:

       id                   Time
0      N01  Thu Sep 10 11:44:30 XYZ 2020
1      V33  Thu Sep 10 11:39:05 ABC 2020
2      N01  Thu Sep 10 11:44:30 XYZ 2020

I am trying to convert Time column to datetime object.我正在尝试将Time列转换为日期Time对象。 If I'm using:如果我使用:

df1['Time'] = pd.to_datetime(df1['Time'])

It is throwing a warning message:它正在抛出警告消息:

UnknownTimezoneWarning: tzname BRT identified but not understood.  Pass `tzinfos` argument in order to correctly return a timezone-aware datetime.  In a future version, this will raise an exception.
  category=UnknownTimezoneWarning)

I am aware that there is a format argument in pd.to_datetime() to pass the input format.我知道pd.to_datetime()中有一个format参数来传递输入格式。 But I don't know what to pass as format to bypass the random strings in the middle of the Time column.但我不知道传递什么format来绕过Time列中间的随机字符串。

Is there any way to correctly get the datetime object from the Time column so that the random strings don't have any effect?有什么方法可以从Time列正确获取 datetime 对象,以便随机字符串没有任何影响?

If you the characters you wants to remove are some following upper cases, you can handle it with a regex function with remove followed uppercase:如果您要删除的字符是以下一些大写字母,则可以使用 remove 后跟大写字母的正则表达式函数来处理它:

import pandas as pd将熊猫导入为 pd

data={'id':['N01','V33','N01'],
      'time':['Thu Sep 10 11:44:30 XYZ 2020','Thu Sep 10 11:39:05 ABC 2020','Thu Sep 10 11:44:30 XYZ 2020']}


df = pd.DataFrame(data)
df['time']=pd.to_datetime(df['time'].str.replace('([A-Z].[A-Z])',''),format=r'%a %b %d %H:%M:%S  %Y')
print(df)

result:结果:

    id                time
0  N01 2020-09-10 11:44:30
1  V33 2020-09-10 11:39:05
2  N01 2020-09-10 11:44:30

暂无
暂无

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

相关问题 将dataframe列从对象转换为date而不是datetime - converting dataframe column from object to date not datetime 如何在 dataframe 日期时间列 pandas 中获取非连续日期时间 - how to get non continuous date time in dataframe datetime column pandas pandas - 将时间和日期从两个 dataframe 列组合到一个日期时间列 - pandas - combine time and date from two dataframe columns to a datetime column 将 pandas dataframe 日期和时间字符串转换为日期时间 - Convert pandas dataframe date and time string into datetime 如何将带有日期、时间和 NaN 的对象列转换为 Python Pandas DataFrame 中的 datetime64 列? - How to convert object column with date, time and NaN to datetime64 column in DataFrame in Python Pandas? 如何仅从具有日期时间值的数据框列中获取日期 - how to get only date from dataframe column with date time value 将数据框中的对象(时间)类型列转换为日期时间 - Convert object (time) type column in dataframe to datetime 获取 pandas dataframe 中的所有列,当不同列中存在不同时区时,该列是日期列 - Get all columns in a pandas dataframe that is a date-column when different time-zones are present in different columns 当时间戳中仅存在时间时,如何在日期时间对象中添加日期 - How to add the date in a datetime object when only time is present in timestamp 在熊猫数据框中将datetime64列拆分为日期和时间列 - Split datetime64 column into a date and time column in pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM