1

I am trying to convert my string 'Time' to Time in Python but getting error

Tried multiple things Not Working :- Python datetime formatting without zero-padding

Code:

dt['Time'] = pd.to_datetime(dt['Time'], format= '%H:%M:%S',
infer_datetime_format=True)

Data:

Ticker,Date,Time,Open,High,Low,Close,Volume,OpenInterest
BANKNIFTY04JUN2023000CE.NFO,14/05/2020,13:00:59,65,65,65,65,20,0
BANKNIFTY04JUN2023000CE.NFO,14/05/2020,15:01:59,75,75,75,75,20,20
BANKNIFTY14MAY2013900PE.NFO,14/05/2020,
9:15:59,0.8,0.8,0.7,0.7,40,18860

Error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\Ankur\anaconda3\lib\site-packages\pandas\core\tools\datetimes.py", line 728, in to_datetime
    values = convert_listlike(arg._values, format)
  File "C:\Users\Ankur\anaconda3\lib\site-packages\pandas\core\tools\datetimes.py", line 447, in _convert_listlike_datetimes
    allow_object=True,
  File "C:\Users\Ankur\anaconda3\lib\site-packages\pandas\core\arrays\datetimes.py", line 1861, in objects_to_datetime64ns
    raise e
  File "C:\Users\Ankur\anaconda3\lib\site-packages\pandas\core\arrays\datetimes.py", line 1852, in objects_to_datetime64ns
    require_iso8601=require_iso8601,
  File "pandas\_libs\tslib.pyx", line 481, in pandas._libs.tslib.array_to_datetime
  File "pandas\_libs\tslib.pyx", line 698, in pandas._libs.tslib.array_to_datetime
  File "pandas\_libs\tslib.pyx", line 694, in pandas._libs.tslib.array_to_datetime
  File "pandas\_libs\tslib.pyx", line 649, in pandas._libs.tslib.array_to_datetime
  File "pandas\_libs\tslibs\conversion.pyx", line 399, in pandas._libs.tslibs.conversion.convert_datetime_to_tsobject
  File "pandas\_libs\tslibs\np_datetime.pyx", line 117, in pandas._libs.tslibs.np_datetime.check_dts_bounds
pandas._libs.tslibs.np_datetime.OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-01-01 09:15:59
Ankur Tripathi
  • 671
  • 9
  • 35
  • The error reads as if you try to parse a string `1-01-01 09:15:59` to datetime - that year is out of range for pandas nanosecond timestamp. You could set `errors='coerce'` to skip that date. – FObersteiner Nov 28 '20 at 08:38

1 Answers1

0

Just double checking - is there an erroneous line on the last row? When I copied your data to a text file it had that line break, which will cause you issues, but I corrected that just in case it was StackOverflow formatting it weirdly.

Pandas is usually smart enough to figure out the datetime format automatically, especially if it is a standard format.

Regardless this script works perfectly for me:

import pandas as pd

dt = pd.read_csv('data.csv')
dt['Time'] = pd.to_datetime(dt['Time'], format="%H:%M:%S")

Where data.csv is a copy of the data attached to this question. You can even omit the format key.

kmiller96
  • 198
  • 6