简体   繁体   English

将dataframe列从对象转换为date而不是datetime

[英]converting dataframe column from object to date not datetime

I am using python version 3.7. 我使用的是python版本3.7。

I have a dataframe, df that contains one column called TDate. 我有一个数据帧,df包含一个名为TDate的列。

The column looks like below. 该列如下所示。

 2019-01-01 00:00:00
 2019-01-02 00:00:00
 2019-01-03 00:00:00
 2019-01-04 00:00:00

When I do df.dtypes it tell me the column is of type object. 当我做df.dtypes时它告诉我列是object类型。

I then have the line below, 我接下来就行,

myDates = pd.to_datetime(df['TDate']) myDates = pd.to_datetime(df ['TDate'])

So myDates is a pandas Series. 所以myDates是一个熊猫系列。

However if I access one element of this myDates series and check the type it tells me it is a libs.tslib.timestamps.Timestamp 但是,如果我访问此myDates系列的一个元素并检查其类型,它告诉我它是一个libs.tslib.timestamps.Timestamp

I just want to convert the original dataframe column from an object to a date in the format yyyy-mm-dd. 我只想将原始数据帧列从对象转换为格式为yyyy-mm-dd的日期。 What is the best way of doing this? 这样做的最佳方式是什么?

I looked at converting a timestamp to a date but that didn't work as the timestamp is a string literal not a integer. 我考虑将时间戳转换为日期,但由于时间戳是字符串文字而不是整数,因此不起作用。

Use Series.dt.floor for remove time information from column (or better it is set to default 00:00:00 value in each datetime): 使用Series.dt.floor从列中删除时间信息(或者更好地将其设置为每个日期时间的默认值00:00:00 ):

myDates = pd.to_datetime(df['TDate']).dt.floor('d')
#for save to same column
#df['TDate'] = pd.to_datetime(df['TDate']).dt.floor('d')

Or Series.dt.normalize : 或者Series.dt.normalize

myDates = pd.to_datetime(df['TDate']).dt.normalize()
#for save to same column
#df['TDate'] = pd.to_datetime(df['TDate']).dt.normalize()

If use: 如果使用:

myDates = pd.to_datetime(df['TDate']).dt.date

then output is python dates objects, so most datetimelike functions failed. 然后输出是python日期对象,因此大多数类似datetime的函数都失败了。

此代码段会将时间戳转换为日期

df['TDate']= pd.to_datetime(df['TDate']).dt.date

暂无
暂无

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

相关问题 将 dataframe 列从 Pandas 时间戳转换为日期时间(或 datetime.date) - Converting dataframe column from Pandas Timestamp to datetime (or datetime.date) 将pandas dataframe中的对象列转换为datetime - Converting object column in pandas dataframe to datetime 将日期信息作为对象的列转换为日期时间的问题 - Problem converting column with date info as object to datetime 将数据框中的列转换为日期时间 - Converting column in Dataframe to datetime Dataframe - 将整列从 str object 转换为 datetime object - 类型错误:strptime() 参数 1 必须是 str,而不是 Series - Dataframe - Converting entire column from str object to datetime object - TypeError: strptime() argument 1 must be str, not Series 将熊猫数据框转换为字典时,将日期从字符串更改为日期时间对象 - Change in date from string to datetime object when converting pandas dataframe to dictionary 将 MM:SS 格式的 Pandas Dataframe 列 object 转换为日期时间类型? - Converting Pandas Dataframe column object in MM:SS format to Datetime type? 将 dataframe 列从 object 转换为 timedelta 并求和 - Converting dataframe column from object to timedelta and summing 从数据框列中获取正确的日期时间对象,其中包含带有日期和时间的随机字符串 - Get correct datetime object from dataframe column with random string present with date and time pandas dataframe 中的逻辑索引,带有时间戳列和 datetime.date-object - logical indexing in pandas dataframe with timestamp column and datetime.date-object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM