如何在Python中从我的日期时间列中移除时间。

huangapple go评论62阅读模式
英文:

How to remove time from my date time column in python

问题

I am a beginner in python and need some guidance.
我是Python的初学者,需要一些指导。

I read in several worksheets from a single Excel Workbook and appended them into a single dataframe in Python. However, the output file has changed my date column which is still defined as an object into a different date format and added time, see example below:
我从单个Excel工作簿中读取了多个工作表,并将它们附加到一个Python的数据框中。然而,输出文件已更改我的日期列,仍然被定义为对象,转换成了不同的日期格式并添加了时间,如下所示:

Original date format Date format outputed from Python
02/03/1992 1992-02-03 00:00:00

This is the code I used to append the worksheets
这是我用来附加工作表的代码:

import pandas as pd

# First, combine all the pages in each Workbook into one sheet

df_toAppend = pd.concat(pd.read_excel('C:/Sam/Predicting Outcomes .xlsx', sheet_name=None), ignore_index=True)

I have tried to just extract the date from the column, but it says its needs to be a date stamp.
我尝试仅从列中提取日期,但它说它需要一个日期时间戳。

I have tried the following to extract just the date part:
我尝试了以下方法仅提取日期部分:

df['Date_of_Birth']=df['Date_of_Birth'].dt.date

but get the following error: AttributeError: Can only use .dt accessor with datetimelike values.
但是出现以下错误:AttributeError: Can only use .dt accessor with datetimelike values.

I have also tried the following:
我还尝试了以下方法:

df['Date_of_Birth'] = pd.to_datetime(df['Date_of_Birth'], format='%Y%M%d')

But the error message below is returned: ValueError: time data '(Timestamp('1982-06-10 00:00:00'), '%d%b%Y:%H:%M:%S.%f')' does not match format '%Y%M%d' (match).
但是返回以下错误消息:ValueError: time data '(Timestamp('1982-06-10 00:00:00'), '%d%b%Y:%H:%M:%S.%f')' does not match format '%Y%M%d' (match)。

I have also tried the following to split the time from the date and then try, but the error message says: AttributeError: Can only use .str accessor with string values!
我还尝试了以下方法来分离日期和时间,然后尝试,但错误消息说:AttributeError: Can only use .str accessor with string values!

pd.to_datetime(df['Date_of_Birth'].str.split(':', n=1).str[0])

Python is saying my Date_of_Birth column is an object.
Python说我的Date_of_Birth列是一个对象。

I just want Python to return my dates column as how I originally inputted them. I do not want it to change the order of days, months or years or to add a time.
我只想让Python返回我的日期列,就像我最初输入它们一样。我不希望它更改日期、月份或年份的顺序,也不希望添加时间。

英文:

I am a beginner in python and need some guidance.
I read in several worksheets from a single Excel Workbook and appended them into a single dataframe in Python. However, the output file has changed my date column which is still defined as an object into a different date format and added time, see example below:

Original date format Date format outputed from Python
02/03/1992 1992-02-03 00:00:00

This is the code I used to append the worksheets

*import pandas as pd

# First, combine all the pages in each Workbook into one sheet

df_toAppend = pd.concat(pd.read_excel('C:/Sam/Predicting Outcomes .xlsx', sheet_name=None), ignore_index=True)*

I have tried to just extract the date from the column, but it says its needs to be a date stamp

I have tried the following to extract just the date part:

df['Date_of_Birth']=df['Date_of_Birth'].dt.date

but get the following error: AttributeError: Can only use .dt accessor with datetimelike values

I have also tried the following:

df['Date_of_Birth'] = pd.to_datetime(df['Date_of_Birth'], format ='%Y%M%d')

But the error message below is returned:
ValueError: time data '(Timestamp('1982-06-10 00:00:00'), '%d%b%Y:%H:%M:%S.%f')' does not match format '%Y%M%d' (match)

I have also tried the following to split the time from the date and then try, but the error message says: AttributeError: Can only use .str accessor with string values!

pd.to_datetime(df['Date_of_Birth'].str.split(':', n=1).str[0])

Python is saying my Date_of_Birth column is an object.
I just want Python to return my dates column as how I originally inputted them. I do not want it to change the order of days, months or years or to add a time.
Thanks

答案1

得分: 1

将类型更改为日期时间,并使用之前使用的相同逻辑。

# 将 'Date_of_Birth' 列转换为日期时间
df['Date_of_Birth'] = pd.to_datetime(df_combined['Date_of_Birth'])

# 将 'Date_of_Birth' 列以原始格式转换回字符串
df['Date_of_Birth'] = df_combined['Date_of_Birth'].dt.strftime('%m/%d/%Y')
英文:

Change the type to the date time and use the same logic you used before

# Convert the 'Date_of_Birth' column to datetime
df['Date_of_Birth'] = pd.to_datetime(df_combined['Date_of_Birth'])

# Convert the 'Date_of_Birth' column back to string with the original format
df['Date_of_Birth'] = df_combined['Date_of_Birth'].dt.strftime('%m/%d/%Y')

huangapple
  • 本文由 发表于 2023年5月21日 22:40:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定