python - Convert '%m/%d/%Y' string index into pandas datetime index -
my index datetime string format '%m/%d/%y' ('09/26/2007')
when try convert index datetime index using pd.to_datetime
function pd.to_datetime(df.index)
, got error message outofboundsdatetime: out of bounds nanosecond timestamp: 1-01-01 00:00:00
it looks pandas can't detect right string format, how can convert index datetime index?
thanks
the of error message, appears may have string '1/1/0001'
in index. example,
df = pd.dataframe([1,2], index=['09/26/2007', '1/1/0001']) pd.to_datetime(df.index)
raises
outofboundsdatetime: out of bounds nanosecond timestamp: 1-01-01 00:00:00
this error arises because datetimeindex uses array of numpy datetime64[ns]
s can not represent date 0001-01-01. datetime64[ns]
dtype can represent dates in range [1678 ad, 2262 ad]
.
there pandas github issue discussing limitation.
for now, recommended solution use periodindex instead of datetimeindex:
df = pd.dataframe([1,2], index=['09/26/2007', '1/1/0001']) df.index = pd.periodindex(df.index, freq='d')
yields
0 2007-09-26 1 1-01-01 2
Comments
Post a Comment