0

How do I format the following string to shorter format. I have a list of strings that have the following shape

2022-06-03T17:00:00.000000000
2022-06-04T09:40:00.000000000
2022-06-05T02:20:00.000000000
2022-06-05T19:00:00.000000000
2022-06-06T11:40:00.000000000
2022-06-07T04:20:00.000000000
2022-06-07T21:00:00.000000000
2022-06-08T13:40:00.000000000
2022-06-09T06:20:00.000000000

They're taken from ax.get_xticklabels() since I want to format xticklabels().

I want the following format: e.g. 2022-06-09 17:00.

Now, I could use the following trick loop over each and tick.get_text()[:16].replace('T', ' ') which gives me:

2022-06-03 17:00
2022-06-04 09:40
2022-06-05 02:20
2022-06-05 19:00
2022-06-06 11:40
2022-06-07 04:20
2022-06-07 21:00
2022-06-08 13:40
2022-06-09 06:20

But is there another, cleaner, simpler, way?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Geosphere
  • 315
  • 4
  • 15
  • 1
    Have you tried using `strptime` and `strftime`? Maybe just configure the axis ticks to use the correct format to begin with? – mkrieger1 Jun 10 '22 at 09:09
  • I have tried but it failed. I wanted to reduce the frequency of ticks and the only way that works in Seaborn was to set the frequency this way: https://stackoverflow.com/questions/72560761/seaborn-heatmap-setting-frequency-of-datetime-ytick-labels Because of that using strftime didn't work. – Geosphere Jun 10 '22 at 09:12
  • What you have shown is about *where* the ticks are, not about how they are formatted. – mkrieger1 Jun 10 '22 at 09:18
  • There are examples for how to specify the format [here](https://stackoverflow.com/questions/67237923/reformat-seaborn-axis-tick-labels-datetime-and-scientific-notation) and [here](https://stackoverflow.com/questions/51105648/ordering-and-formatting-dates-on-x-axis-in-seaborn-bar-plot). – mkrieger1 Jun 10 '22 at 09:20
  • I tried to explain the origin of the problem, where it all started and why I arrived to where I did. – Geosphere Jun 10 '22 at 09:22
  • The thing is in Seaborn heatmap I can't specify the index as for a barchart with `strftime`. Moreover, the index in the dataframe I'm using had the format that I already wanted but for some reason in Seaborn it was altered. – Geosphere Jun 10 '22 at 09:26
  • Assuming you are using pandas, the easiest option is to set the format in the dataframe column before plotting the heatmap. – Trenton McKinney Jun 10 '22 at 17:25

1 Answers1

0
import dateutil.parser


dt_strs = [
'2022-06-03T17:00:00.000000000',
'2022-06-04T09:40:00.000000000',
'2022-06-05T02:20:00.000000000',
'2022-06-05T19:00:00.000000000',
'2022-06-06T11:40:00.000000000',
'2022-06-07T04:20:00.000000000',
'2022-06-07T21:00:00.000000000',
'2022-06-08T13:40:00.000000000',
'2022-06-09T06:20:00.000000000',
]

for dt_str in dt_strs:
    print(dateutil.parser.isoparse(dt_str).strftime("%Y-%M-%d %H:%M"))

Output:

2022-00-03 17:00
2022-40-04 09:40
2022-20-05 02:20
2022-00-05 19:00
2022-40-06 11:40
2022-20-07 04:20
2022-00-07 21:00
2022-40-08 13:40
2022-20-09 06:20
Russel FP
  • 125
  • 3