dft
dft.to_csv('datatypes_test_file.csv',index=False)
dfc = pd.read_csv('datatypes_test_file.csv')
dfc.dtypes
pandas provides various ways to specify column data types.
# Indicate the data types for columns
dft = pd.read_csv('datatypes_test_file.csv',
dtype={
"str2": "string",
"str3": "category",
"float32": np.float32,
"bool": np.bool
},
parse_dates=['date1', 'date2'],
converters={'delta': pd.to_timedelta})
# Explicitly convert dtype.
dft['str2'].astype('string', copy=False)
# Set invalid to nan and smallest dtype.
dft['int8'] = pd.to_numeric(dft['int8'], errors="coerce",
downcast="integer")
# Might be faster if date format is not standard.
dft['date3'] = pd.to_datetime(dft['date3'], format='%Y-%m-%d')
# Explicit conversion of the column. Might be faster than using a converter.
dft['delta'] = pd.to_timedelta(dft['delta'])
dft