When I want to convert a column of dates in the data frame into the date format of panda, I encountered this kind of error.
reader = pd.read_csv(f'new_files/2020-12-22-5-10.csv', usecols=['passCarTime'],dtype={'passCarTime':'string'})
pd.to_datetime(reader.passCarTime.head())
Out[98]:
0 2020-12-22 10:00:00
1 2020-12-22 10:00:00
2 2020-12-22 10:00:00
3 2020-12-22 10:00:00
4 2020-12-22 10:00:00
Name: passCarTime, dtype: datetime64[ns]
pd.to_datetime(reader.passCarTime)
Traceback (most recent call last):
File "D:\PyCharm2020\python2020\lib\site-packages\pandas\core\arrays\datetimes.py", line 2085, in objects_to_datetime64ns
values, tz_parsed = conversion.datetime_to_datetime64(data)
File "pandas\_libs\tslibs\conversion.pyx", line 350, in pandas._libs.tslibs.conversion.datetime_to_datetime64
TypeError: Unrecognized value type: <class 'str'>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\PyCharm2020\python2020\lib\site-packages\IPython\core\interactiveshell.py", line 3427, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-99-e1b00dc18517>", line 1, in <module>
pd.to_datetime(reader.passCarTime)
File "D:\PyCharm2020\python2020\lib\site-packages\pandas\core\tools\datetimes.py", line 801, in to_datetime
cache_array = _maybe_cache(arg, format, cache, convert_listlike)
File "D:\PyCharm2020\python2020\lib\site-packages\pandas\core\tools\datetimes.py", line 178, in _maybe_cache
cache_dates = convert_listlike(unique_dates, format)
File "D:\PyCharm2020\python2020\lib\site-packages\pandas\core\tools\datetimes.py", line 465, in _convert_listlike_datetimes
result, tz_parsed = objects_to_datetime64ns(
File "D:\PyCharm2020\python2020\lib\site-packages\pandas\core\arrays\datetimes.py", line 2090, in objects_to_datetime64ns
raise e
File "D:\PyCharm2020\python2020\lib\site-packages\pandas\core\arrays\datetimes.py", line 2075, in objects_to_datetime64ns
result, tz_parsed = tslib.array_to_datetime(
File "pandas\_libs\tslib.pyx", line 364, in pandas._libs.tslib.array_to_datetime
File "pandas\_libs\tslib.pyx", line 591, in pandas._libs.tslib.array_to_datetime
File "pandas\_libs\tslib.pyx", line 726, in pandas._libs.tslib.array_to_datetime_object
File "pandas\_libs\tslib.pyx", line 717, in pandas._libs.tslib.array_to_datetime_object
File "pandas\_libs\tslibs\parsing.pyx", line 243, in pandas._libs.tslibs.parsing.parse_datetime_string
File "D:\PyCharm2020\python2020\lib\site-packages\dateutil\parser\_parser.py", line 1374, in parse
return DEFAULTPARSER.parse(timestr, **kwargs)
File "D:\PyCharm2020\python2020\lib\site-packages\dateutil\parser\_parser.py", line 649, in parse
raise ParserError("Unknown string format: %s", timestr)
dateutil.parser._parser.ParserError: Unknown string format: passCarTime
I’m not a professional, and my English is not good. I can’t understand what’s wrong. I have seen that there is no missing value in the date column of the file, and there is no date that does not conform to the format… This is very strange, welcome to leave a message, thanks in advance!
Solution
When converting, add a parameter errors ='coerce '
.
reader = pd.read_csv(f'new_files/2020-12-22-5-10.csv', usecols=['passCarTime'],dtype={'passCarTime':'string'})
reader.passCarTime = pd.to_datetime(reader.passCarTime,errors='coerce')
reader.passCarTime.head()
Out[120]:
0 2020-12-22 10:00:00
1 2020-12-22 10:00:00
2 2020-12-22 10:00:00
3 2020-12-22 10:00:00
4 2020-12-22 10:00:00
Name: passCarTime, dtype: datetime64[ns]
reader.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 307707 entries, 0 to 307706
Data columns (total 1 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 passCarTime 307703 non-null datetime64[ns]
dtypes: datetime64[ns](1)
memory usage: 2.3 MB
Read More:
- [Solved] python Error: GuessedAtParserWarning: No parser was explicitly specified
- [Solved] Python3.9 Pycropto RSA Error: TypeError: can’t concat str to bytes
- Python PIP TypeError: expected str, bytes or os.PathLike object, not int
- How to Solve args = parser.parse_args() error
- [Solved] TypeError: unsupported operand type(s) for /: ‘str‘ and ‘int‘
- [Solved] ParserError: NULL byte detected. This byte cannot be processed in Python‘s native csv library
- [Solved] Python Error: TypeError: write() argument must be str, not bytes
- Python Pandas Typeerror: invalid type comparison
- Python scatter chart error: TypeError: object of type ‘NoneType’ has no len()
- Windows10 DOTA_devkit Error: TypeError: ‘>=‘ not supported between instances of ‘NoneType‘ and ‘str‘
- Python 3.7 Error: AttributeError: ‘str‘ object has no attribute ‘decode‘ [How to Solve]
- Python: ___ () vs ___ str__()
- Python opens the table and appears pandas.errors.ParserError: Error tokenizing data. C error:
- [Solved] python-sutime Error: the JSON object must be str, bytes or bytearray, not ‘java.lang.String‘
- Pandas read_csv pandas.errors.ParserError: Error tokenizing data
- TypeError: Decimal type object is not a JSON serialization solution
- [resolution] str.contains() problem] valueerror: cannot index with vector containing Na/Nan values
- [Solved] Plugin requires missing class loader for ‘Python‘
- [Solved] TypeError: Object of type ‘bytes’ is not JSON serializable
- Python 3.X error: valueerror: data type must provide an itemsize