0

I am trying to parse this column to a column containing the weekday. Eg. 0-6 or 'Monday', 'Tuesday'...

0    2020-10-06 03:39:51.000000
1    2020-10-06 09:48:10.000000
2    2020-10-06 14:19:27.000000
3    2020-10-05 10:21:22.000000
4    2020-10-05 13:35:06.000000
Name: charging_start, dtype: object

I am sorry for the beginner question, but I am one. I was playing with datetime, but could not make it work. This was my last approach:

from datetime import date

test = '2020-10-06 03:39:51.000000'
print("\n{}\n".format(test))
print(date.fromisoformat(test))

Resulting to:

2020-10-06 03:39:51.000000

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-318-81788367097a> in <module>
     13 
     14 test = '2020-10-06 03:39:51.000000'
---> 15 print(date.fromisoformat(test))

ValueError: Invalid isoformat string: '2020-10-06 03:39:51.000000'
AndreasInfo
  • 1,062
  • 11
  • 26

1 Answers1

1

You can try:

from datetime import datetime

test = '2020-10-06 03:39:51.000000'
print("\n{}\n".format(test))
print(datetime.fromisoformat(test))

Output

2020-10-06 03:39:51.000000

2020-10-06 03:39:51

For this case is better to use the datetime class instead of the date class since you are parsing a datetime value and not just a date.
If the test value was test = '2020-10-06' then the date class fromisoformat() method was parsing the test value.

Leo Arad
  • 4,452
  • 2
  • 6
  • 17
  • Thanks. I am playing with Jupyter notebook and there it was print(datetime..datetime.fromisoformat(test)) – AndreasInfo Jan 13 '21 at 14:04
  • Or print(datetime.datetime.strptime(co_start, "%Y-%m-%d %H:%M:%S.%f")) => probably not suitable for this example, but good to keep in mind. – AndreasInfo Jan 13 '21 at 14:05