I’m trying to set my time record to iso08601 but I get the error that the iso does not match the given time stamp. I also made the format my self (format=’%Y-%m-%d %H:%M:%S’) this didn’t work the way I thought it would. ChatGPT gave me some options but I’ve tried them all. I think its because of the spaces.
This is my input:
print(df.columns.tolist())
print(df.head())
print(df.columns[0])
df['time'] = pd.to_datetime(df['time'], format='ISO8601')
df['time'] = df['time'].dt.tz_localize(None)
df.head()
This is the output
['entity', 'time', 'pmErabEstabSuccInit']
entity time pmErabEstabSuccInit
0 5 2024-03-30T00:15:00+01 3327
1 5 2024-03-30T03:00:00+01 3399
2 5 2024-03-30T03:15:00+01 3340
3 5 2024-03-30T03:30:00+01 3738
4 5 2024-03-30T03:45:00+01 3523
entity
~~~~~~~~~
this is the error:
~~~~~~~
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~/opt/anaconda3/lib/python3.9/site-packages/pandas/core/tools/datetimes.py in _to_datetime_with_format(arg, orig_arg, name, tz, fmt, exact, errors, infer_datetime_format)
508 try:
--> 509 values, tz = conversion.datetime_to_datetime64(arg)
510 dta = DatetimeArray(values, dtype=tz_to_dtype(tz))
~/opt/anaconda3/lib/python3.9/site-packages/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:
ValueError Traceback (most recent call last)
/var/folders/rp/c58fxmmx31nblwvg40bhzb880000gn/T/ipykernel_11664/537408717.py in <module>
2 print(df.head())
3 print(df.columns[0])
----> 4 df['time'] = pd.to_datetime(df['time'], format='ISO8601')
5 df['time'] = df['time'].dt.tz_localize(None)
6
~/opt/anaconda3/lib/python3.9/site-packages/pandas/core/tools/datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)
881 result = result.tz_localize(tz) # type: ignore[call-arg]
882 elif isinstance(arg, ABCSeries):
--> 883 cache_array = _maybe_cache(arg, format, cache, convert_listlike)
884 if not cache_array.empty:
885 result = arg.map(cache_array)
~/opt/anaconda3/lib/python3.9/site-packages/pandas/core/tools/datetimes.py in _maybe_cache(arg, format, cache, convert_listlike)
193 unique_dates = unique(arg)
194 if len(unique_dates) < len(arg):
--> 195 cache_dates = convert_listlike(unique_dates, format)
196 cache_array = Series(cache_dates, index=unique_dates)
197 # GH#39882 and GH#35888 in case of None and NaT we get duplicates
~/opt/anaconda3/lib/python3.9/site-packages/pandas/core/tools/datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
391
392 if format is not None:
--> 393 res = _to_datetime_with_format(
394 arg, orig_arg, name, tz, format, exact, errors, infer_datetime_format
395 )
~/opt/anaconda3/lib/python3.9/site-packages/pandas/core/tools/datetimes.py in _to_datetime_with_format(arg, orig_arg, name, tz, fmt, exact, errors, infer_datetime_format)
511 return DatetimeIndex._simple_new(dta, name=name)
512 except (ValueError, TypeError):
--> 513 raise err
514
515
~/opt/anaconda3/lib/python3.9/site-packages/pandas/core/tools/datetimes.py in _to_datetime_with_format(arg, orig_arg, name, tz, fmt, exact, errors, infer_datetime_format)
498
499 # fallback
--> 500 res = _array_strptime_with_fallback(
501 arg, name, tz, fmt, exact, errors, infer_datetime_format
502 )
~/opt/anaconda3/lib/python3.9/site-packages/pandas/core/tools/datetimes.py in _array_strptime_with_fallback(arg, name, tz, fmt, exact, errors, infer_datetime_format)
434
435 try:
--> 436 result, timezones = array_strptime(arg, fmt, exact=exact, errors=errors)
437 if "%Z" in fmt or "%z" in fmt:
438 return _return_parsed_timezone_results(result, timezones, tz, name)
~/opt/anaconda3/lib/python3.9/site-packages/pandas/_libs/tslibs/strptime.pyx in pandas._libs.tslibs.strptime.array_strptime()
ValueError: time data '2024-03-30T00:15:00+01' does not match format 'ISO8601' (match)
I tried adding the T, this also didn’t work.
user25438820 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
I think you should use use isoformat()
pd.Timestamp('2024-03-30T00:15:00+01').isoformat()