2

I have the following data in my Date Column,

enter image description here

When I used to convert this column into Standard Date Time format of pandas using:

AQI_TS["Date"] = pd.to_datetime(AQI_TS["Date"])

It is giving the following error:

TypeError                                 Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\arrays\datetimes.py in objects_to_datetime64ns(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object)
   2084         try:
-> 2085             values, tz_parsed = conversion.datetime_to_datetime64(data)
   2086             # If tzaware, these values represent unix timestamps, so we

pandas\_libs\tslibs\conversion.pyx in pandas._libs.tslibs.conversion.datetime_to_datetime64()

TypeError: Unrecognized value type: <class 'str'>

During handling of the above exception, another exception occurred:

ParserError                               Traceback (most recent call last)
<ipython-input-28-2a070dee7e91> in <module>
----> 1 AQI_TS["Date"] = pd.to_datetime(AQI_TS["Date"])

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)
    799                 result = result.tz_localize(tz)
    800     elif isinstance(arg, ABCSeries):
--> 801         cache_array = _maybe_cache(arg, format, cache, convert_listlike)
    802         if not cache_array.empty:
    803             result = arg.map(cache_array)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py in _maybe_cache(arg, format, cache, convert_listlike)
    176         unique_dates = unique(arg)
    177         if len(unique_dates) < len(arg):
--> 178             cache_dates = convert_listlike(unique_dates, format)
    179             cache_array = Series(cache_dates, index=unique_dates)
    180     return cache_array

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
    463         assert format is None or infer_datetime_format
    464         utc = tz == "utc"
--> 465         result, tz_parsed = objects_to_datetime64ns(
    466             arg,
    467             dayfirst=dayfirst,

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\arrays\datetimes.py in objects_to_datetime64ns(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object)
   2088             return values.view("i8"), tz_parsed
   2089         except (ValueError, TypeError):
-> 2090             raise e
   2091 
   2092     if tz_parsed is not None:

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\arrays\datetimes.py in objects_to_datetime64ns(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object)
   2073 
   2074     try:
-> 2075         result, tz_parsed = tslib.array_to_datetime(
   2076             data,
   2077             errors=errors,

pandas\_libs\tslib.pyx in pandas._libs.tslib.array_to_datetime()

pandas\_libs\tslib.pyx in pandas._libs.tslib.array_to_datetime()

pandas\_libs\tslib.pyx in pandas._libs.tslib.array_to_datetime_object()

pandas\_libs\tslib.pyx in pandas._libs.tslib.array_to_datetime_object()

pandas\_libs\tslibs\parsing.pyx in pandas._libs.tslibs.parsing.parse_datetime_string()

C:\ProgramData\Anaconda3\lib\site-packages\dateutil\parser\_parser.py in parse(timestr, parserinfo, **kwargs)
   1372         return parser(parserinfo).parse(timestr, **kwargs)
   1373     else:
-> 1374         return DEFAULTPARSER.parse(timestr, **kwargs)
   1375 
   1376 

C:\ProgramData\Anaconda3\lib\site-packages\dateutil\parser\_parser.py in parse(self, timestr, default, ignoretz, tzinfos, **kwargs)
    647 
    648         if res is None:
--> 649             raise ParserError("Unknown string format: %s", timestr)
    650 
    651         if len(res) == 0:

ParserError: Unknown string format: 2020_09_01 

I have also used the coerce=True but all the values under this column Date gets turned into NAT, While my desired output is to have date in this format 2020/09/01 or 01/09/2020, Looking for a solution, how can i get rid-off from above error.

2
  • 2
    Try: AQI_TS["Date"] = pd.to_datetime(AQI_TS["Date"], format='%Y_%m_%d')
    – MDR
    Commented Aug 2, 2021 at 9:25
  • Please add sample data from your date column. There appear to be more than one format there. Commented Aug 2, 2021 at 9:26

1 Answer 1

3

You need to pass the format string also i.e. %Y_%m_%d for your sample data.

>>> pd.to_datetime(AQI_TS['Date'], format='%Y_%m_%d')

0   2020-09-01
1   2020-09-01
2   2020-09-01
3   2020-09-01
4   2020-09-01
5   2020-09-01
6   2020-09-01
Name: Date, dtype: datetime64[ns]
3
  • Thank you it is working :) , one more thing please how can I replace dashes '-' to '/' ?
    – Filbadeha
    Commented Aug 2, 2021 at 9:28
  • 1
    That's how pandas represent the dates by default, if you want to have delimiter as / instead of -, then you need to apply the corresponding format using Series.dt.strftime which will convert the datetime column to a string type.
    – ThePyGuy
    Commented Aug 2, 2021 at 9:29
  • 1
    i.e. pd.to_datetime(AQI_TS['Date'], format='%Y_%m_%d').dt.strftime('%Y/%m/%d')
    – ThePyGuy
    Commented Aug 2, 2021 at 9:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.