I'm working with a dataframe that shows historical prices for electricity. I'm wanting to convert the 'Months' column to datetime. I'm using the to_datetime
to do this. However, when I do this, all the dates return as NaT
. Specifically, the dataframe goes from this:
Month ... PEA
0 Oct-22 ... 0.249
1 Sep-22 ... 0.021
2 Aug-22 ... -0.048
3 Jul-22 ... -0.053
4 Jun-22 ... -0.032
to looking like this:
Month ... PEA
0 NaT ... NaN
1 NaT ... NaN
2 NaT ... NaN
3 NaT ... NaN
4 NaT ... NaN
My code looks like this:
import numpy as np
import pandas as pd
inputpath='HistoricalPrices'
dataset=pd.read_csv(inputpath,sep=',', dtype={'a': str},low_memory=False)
print(dataset.head())
dataset = dataset.iloc[::-1].reset_index(drop=True)
dataset['Month']=pd.to_datetime(dataset['Month'], errors='coerce', format='%m-%y')
print(dataset['Month'].dtypes)
print(dataset)
The to_datetime
does change the Month
column from object
to datetime64[ns]
but all the values in the Month
column return NaT
. How do I fix this? Am I doing something wrong within the to_datetime
function? I appreciate any help!
CodePudding user response:
try this format: format='%b-%y'