When parsing a CSV file with Pandas the columns that contain datetime
are assigned type object
by default.
How do I make sure the first column have the correct type assigned in this example?
import pandas as pd
import datetime as datetime
data = pd.read_csv("scans.csv")
# dtypes = {
# 'date': datetime,
# 'muscle': str,
# 'side': str,
# 'MQ(0-100)': float,
# 'MQ(raw)': int,
# 'fat': float
# }
# data = pd.read_csv("scans.csv", dtype=dtypes)
print(data.head())
print(data.dtypes)
Here is the console output
date muscle side MQ(0-100) MQ(raw) fat
0 12/16/2018 16:08 glutes R 99.7 154 8.6
1 12/16/2018 16:08 total R 81.8 129 17.0
2 12/16/2018 16:04 glutes L 98.1 140 10.8
3 12/16/2018 16:03 upper_back R 70.2 132 11.6
4 12/16/2018 16:02 upper_back L 77.8 136 11.4
date object
muscle object
side object
MQ(0-100) float64
MQ(raw) int64
fat float64
dtype: object
Error when running the full code
/Users/Developer/PycharmProjects/Sculpt/venv/bin/python /Users/Developer/PycharmProjects/Sculpt/script.py
Traceback (most recent call last):
File "/Users/Developer/PycharmProjects/Sculpt/venv/lib/python3.8/site-packages/pandas/core/dtypes/common.py", line 2050, in pandas_dtype
npdtype = np.dtype(dtype)
TypeError: data type not understood
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/Developer/PycharmProjects/Sculpt/script.py", line 18, in <module>
data = pd.read_csv("scans.csv", dtype=dtypes)
File "/Users/Developer/PycharmProjects/Sculpt/venv/lib/python3.8/site-packages/pandas/io/parsers.py", line 685, in parser_f
return _read(filepath_or_buffer, kwds)
File "/Users/Developer/PycharmProjects/Sculpt/venv/lib/python3.8/site-packages/pandas/io/parsers.py", line 457, in _read
parser = TextFileReader(fp_or_buf, **kwds)
File "/Users/Developer/PycharmProjects/Sculpt/venv/lib/python3.8/site-packages/pandas/io/parsers.py", line 895, in __init__
self._make_engine(self.engine)
File "/Users/Developer/PycharmProjects/Sculpt/venv/lib/python3.8/site-packages/pandas/io/parsers.py", line 1135, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "/Users/Developer/PycharmProjects/Sculpt/venv/lib/python3.8/site-packages/pandas/io/parsers.py", line 1917, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas/_libs/parsers.pyx", line 490, in pandas._libs.parsers.TextReader.__cinit__
File "/Users/Developer/PycharmProjects/Sculpt/venv/lib/python3.8/site-packages/pandas/core/dtypes/common.py", line 2054, in pandas_dtype
raise TypeError("data type not understood")
TypeError: data type not understood
Here is a one-liner solution:
data = pd.read_csv("scans.csv", parse_dates=['date'])
Now getting a good result:
date datetime64[ns]
muscle object
side object
MQ(0-100) float64
MQ(raw) int64
fat float64
dtype: object
The "muscle" and "side" columns are the correct dtype. Pandas treats strings as object dtypes. This can be read here: pandas documentation
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.