[dataframe中的ValueError尝试使用datetime python库提取日,月和年时

问题描述 投票:0回答:1

我在数据框中有三列:Tweet发布时间(UTC),Tweet内容和Tweet位置。 “推文发布时间(UTC)”列中的日期对象的格式为:2020年3月31日10:49:01

我的目标是重新格式化数据框,以使“推文发布时间(UTC)”列仅显示日,月和年(例如31-03-2020),以便能够绘制时间序列图,但我的尝试导致以下错误。

ValueError:time data'0 31 Mar 2020 10:49:01 \ n1 2020年3月31日05:48:43 \ n2 2020年3月30日05:38:50 \ n3 2020年3月29日21:19:23 \ n4 Mar 2020 20:28:22 \ n ... \ n2488 2018年1月2日13:36:07 \ n2489 2018年1月2日10:33:21 \ n2490 2018年1月1日12:23:47 \ n2491 2018年1月1日06:03: 51 \ n2492 2018年1月1日02:09:15 \ n名称:Tweet发表时间(UTC),长度:2451,dtype:object'与格式'%d%b%Y%H:%M:%S'不匹配>

我的代码在下面,请您告诉我我做错了吗?

from datetime import datetime
import pandas as pd
import re #regular expression
from textblob import TextBlob
import string
import preprocessor as p


pd.set_option("expand_frame_repr", False)

df1 = pd.read_csv("C:/tweet_data.csv")

dataType = df1.dtypes
print(dataType)

# convert datetime object to string
old_formatDate = str(df1['Tweet Posted Time (UTC)'])

# extract day, month, and year and convert back to datetime object
date_TimeObject = datetime.strptime(old_formatDate, '%d %b %Y %H:%M:%S')
new_formatDate = date_TimeObject.strftime('%d-%m-%Y')
print(new_formatDate)

我在数据框中有三列:Tweet发布时间(UTC),Tweet内容和Tweet位置。 “推文发布时间(UTC)”列中的日期对象的格式为:2020年3月31日10:49:01我的目标...

python twitter nlp
1个回答
0
投票

我通过将数据框更改为熊猫系列,然后更改为日期时间格式来研究并解决了该问题。然后,应用dt.strftime。

© www.soinside.com 2019 - 2024. All rights reserved.