pandas python pandas日期时间转换为日期

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

python pandas date time conversion to date

pythondatedatetimepandas

提问by Krisjay

I am looking to convert datetime to date for a pandasdatetimeseries.

我希望将日期时间转换为pandasdatetime系列的日期。

I have listed the code below:

我在下面列出了代码:

df = pd.DataFrame()

df = pandas.io.parsers.read_csv("TestData.csv", low_memory=False)

df['PUDATE'] = pd.Series([pd.to_datetime(date) for date in df['DATE_TIME']])

df['PUDATE2'] = datetime.datetime.date(df['PUDATE'])  #Does not work

Can anyone guide me in right direction?

谁能指导我正确的方向?

回答by maxymoo

You can access the datetimemethods of a Pandas series by using the .dtmethods (in a aimilar way to how you would access string methods using .str. For your case, you can extract the date of your datetime column as:

您可以使用这些datetime方法访问Pandas 系列的.dt方法(与使用 访问字符串方法的方式类似.str。对于您的情况,您可以将日期时间列的日期提取为:

df['PUDATE'].dt.date

回答by Sujit Bhattacharyya

This is a simple way to get day of month, from a pandas

这是从Pandas中获取月份中的某一天的简单方法

    #create a dataframe with dates as a string

    test_df = pd.DataFrame({'dob':['2001-01-01', '2002-02-02', '2003-03-03', '2004-04-04']})

    #convert column to type datetime
    test_df['dob']= pd.to_datetime(test_df['dob'])

    # Extract day, month , year  using dt accessor
    test_df['DayOfMonth']=test_df['dob'].dt.day
    test_df['Month']=test_df['dob'].dt.month
    test_df['Year']=test_df['dob'].dt.year

回答by Alex J

I think you need to specify the format for example df['PUDATE2']=datetime.datetime.date(df['PUDATE'], format='%Y%m%d%H%M%S')

我认为您需要指定格式,例如 df['PUDATE2']=datetime.datetime.date(df['PUDATE'], format='%Y%m%d%H%M%S')

So you just need to know what format you are using

所以你只需要知道你使用的是什么格式