lockey
lockey

Reputation: 71

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

{'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?

Upvotes: 2

Views: 451

Answers (3)

Manuel
Manuel

Reputation: 113

You can add this line before parsing datafrmae to dictionary:

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')

Upvotes: 0

Seabass77
Seabass77

Reputation: 187

To convert a DateTime object to a string you can use 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

You more information on FORMAT_STRING see: https://www.w3schools.com/python/python_datetime.asp

Upvotes: 2

Rakesh
Rakesh

Reputation: 82765

Use 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}}

Upvotes: 2

Related Questions