datetime conversion

Asked 2 years ago, Updated 2 years ago, 17 views

charttime   
0   2180-03-16 01:30:00 96.0    
1   2180-03-16 01:45:00 93.0    
2   2180-03-16 02:00:00 90.0    
3   2180-03-16 03:00:00 89.0    
4   2180-03-16 04:00:00 86.0    
... ... ... ... ... ... ... ...
99  2180-03-19 14:00:00 76.0    
100 2180-03-19 15:00:00 71.0    
101 2180-03-19 16:00:00 68.0    
102 2180-03-19 17:00:00 70.0    
103 2180-03-19 20:00:00 76.0    

I want to change the format of these charts to %m-%d%H:%M format.

df['charttime'] = pd.to_datetime(df['charttime'])
df['charttime'] = df['charttime'].dt.strftime('%m-%d %H:%M')

I took advantage of that. The problem is that it became an object when I used strftime. Is there a way to change it back to the datetime format?

Or is there a way to remove the year without using strftime, but keep the datetime format?

python

2022-09-20 17:44

2 Answers

If I were you, I'd leave the exact date time column and make another string-type column that was converted to the required form. (Change the series to a data frame first.) Due to whether or not there is a month-date without a year, the time difference is not calculated accurately.

If you don't need the year in the graph, you can just format and show it when you display the graph, and if you want to calculate by date, you can make a column like dt_month_day and group it based on that value.


2022-09-20 17:44

As far as I know, if you do strftime, you will receive str objects.

If you want to receive data in datetime format, you can convert it to a datetime object using the datetime (year = int, month = int, day = int) command, or keep the object separately before strftime conversion.


2022-09-20 17:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.