I have the following column in a dataframe it is already imported from Mysql DB :
atst
b'2017-01-17 01:50:00'
b'2017-01-17 01:50:00'
b'2017-01-17 01:50:00'
i need to convert it to datetime, i have already tried this approach but it puts Nat in column:
df1['atst']=df['atst'].str.decode("utf-8")
df1['atst']=pd.to_datetime(df1['atst'])
the reason i decode to UTF-8 is that when i try to convert it without decoding i get the following error:
Unknown string format
The contents of your column are composed of strings and not byte-strings as such and hence there is no need to decode from UTF-8 anymore as they are already decoded.
Instead, slice the elements from the first index onwards till the end by treating them like any other string. (note that 0 indicates the first char present in the string which gets omitted as a result)
pd.to_datetime(df['atst'].str.slice(1))
0 2017-01-17 01:50:00
1 2017-01-17 01:50:00
2 2017-01-17 01:50:00
Name: atst, dtype: datetime64[ns]
The str.decode step would have worked correctly had your dataframe been assembled in the following format:
# note b is prefixed in front of quotes
d = pd.DataFrame(dict(atst_mod=[b"2017-01-17 01:50:00", b"2017-01-17 01:50:00",
b"2017-01-17 01:50:00"]))
pd.to_datetime(d['atst_mod'].str.decode("utf-8"))
0 2017-01-17 01:50:00
1 2017-01-17 01:50:00
2 2017-01-17 01:50:00
Name: atst_mod, dtype: datetime64[ns]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With