简体   繁体   English

如何在Python中将日期时间转换为字符串?

[英]How to Convert Datetime to String in Python?

I want to make a line chart by this code : 我想通过此代码制作折线图:

df = pd.DataFrame.from_dict({ 'sentencess' : sentencess, 'publishedAts' : publishedAts, 'hasil_sentimens' : hasil_sentimens })
df.to_csv('chart.csv')

df['publishedAts'] = pd.to_datetime(df['publishedAts'], errors='coerce')

by_day_sentiment = df.groupby([pd.Grouper(key='publishedAts',freq='D'),'hasil_sentimens']).size().unstack('hasil_sentimens')

sentiment_dict = by_day_sentiment.to_dict('dict')

and the output from sentiment_dict is 而sendiment_dict的输出是

{'Negatif ': {Timestamp('2019-08-26 00:00:00', freq='D'): 2.0, Timestamp('2019-08-27 00:00:00', freq='D'): 4.0, Timestamp('2019-08-28 00:00:00', freq='D'): 2.0, Timestamp('2019-08-29 00:00:00', freq='D'): 3.0}, 'Netral ': {Timestamp('2019-08-26 00:00:00', freq='D'): 1.0, Timestamp('2019-08-27 00:00:00', freq='D'): 3.0, Timestamp('2019-08-28 00:00:00', freq='D'): 1.0, Timestamp('2019-08-29 00:00:00', freq='D'): 3.0}, 'Positif ': {Timestamp('2019-08-26 00:00:00', freq='D'): nan, Timestamp('2019-08-27 00:00:00', freq='D'): nan, Timestamp('2019-08-28 00:00:00', freq='D'): nan, Timestamp('2019-08-29 00:00:00', freq='D'): 1.0}}

From that sentiment_dict, how to make a new dict but the key (which is now datetime) is changed to a string? 从该sentiment_dict,如何制作一个新的dict,但密钥(现在为datetime)更改为字符串?

Use strftime('%Y-%m-%d %H:%M:%S') 使用strftime('%Y-%m-%d %H:%M:%S')

Ex: 例如:

from pandas import Timestamp
from numpy import nan
data = {'Negatif ': {Timestamp('2019-08-26 00:00:00', freq='D'): 2.0, Timestamp('2019-08-27 00:00:00', freq='D'): 4.0, Timestamp('2019-08-28 00:00:00', freq='D'): 2.0, Timestamp('2019-08-29 00:00:00', freq='D'): 3.0}, 'Netral ': {Timestamp('2019-08-26 00:00:00', freq='D'): 1.0, Timestamp('2019-08-27 00:00:00', freq='D'): 3.0, Timestamp('2019-08-28 00:00:00', freq='D'): 1.0, Timestamp('2019-08-29 00:00:00', freq='D'): 3.0}, 'Positif ': {Timestamp('2019-08-26 00:00:00', freq='D'): nan, Timestamp('2019-08-27 00:00:00', freq='D'): nan, Timestamp('2019-08-28 00:00:00', freq='D'): nan, Timestamp('2019-08-29 00:00:00', freq='D'): 1.0}}

print({k: {m.strftime('%Y-%m-%d %H:%M:%S'): v for m, v in v.items()} for k, v in data.items()})

Output: 输出:

{'Negatif ': {'2019-08-26 00:00:00': 2.0,
              '2019-08-27 00:00:00': 4.0,
              '2019-08-28 00:00:00': 2.0,
              '2019-08-29 00:00:00': 3.0},
 'Netral ': {'2019-08-26 00:00:00': 1.0,
             '2019-08-27 00:00:00': 3.0,
             '2019-08-28 00:00:00': 1.0,
             '2019-08-29 00:00:00': 3.0},
 'Positif ': {'2019-08-26 00:00:00': nan,
              '2019-08-27 00:00:00': nan,
              '2019-08-28 00:00:00': nan,
              '2019-08-29 00:00:00': 1.0}}

To convert a DateTime object to a string you can use DateTime.strftime(FORMAT_STRING): 要将DateTime对象转换为字符串,可以使用DateTime.strftime(FORMAT_STRING):

import datetime

x = datetime.datetime.now()

print(x.strftime("%H:%M:%S")) 

You can try it here https://repl.it/repls/ImmaterialAlarmingGenre 您可以在这里尝试https://repl.it/repls/ImmaterialAlarmingGenre

You more information on FORMAT_STRING see: https://www.w3schools.com/python/python_datetime.asp 有关FORMAT_STRING的更多信息,请参见: https : //www.w3schools.com/python/python_datetime.asp

You can add this line before parsing datafrmae to dictionary: 您可以在将datafrmae解析为字典之前添加以下行:

by_day_sentiment = df.groupby([pd.Grouper(key='publishedAts',freq='D'),'hasil_sentimens']).size().unstack('hasil_sentimens')

by_day_sentiment['publishedAts'] = by_day_sentiment['publishedAts'].astype(object)

sentiment_dict = by_day_sentiment.to_dict('dict')

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM