PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, August 26, 2022

[FIXED] How to keep the same datatype after saving data to CSV file?

 August 26, 2022     csv, dataframe, pandas, python     No comments   

Issue

I'm saving data to CSV files using pandas. I have one important column with dtype: datetime64[ns].Somehow the datatype is changed to object when I read the data back from CSV file. How can I write, read while keeping the same datatype? Is this related to the encoding type?

df = pd.io.sql.read_sql(sql, cnxn)
df.to_csv(fileName)
df.TimeSeries

Name: TimeSeries, Length: 10000, dtype: datetime64[ns]

DF = pd.read_csv(fileName, sep=',')
DF.TimeSeries

Name: TimeSeries, Length: 10000, dtype: object


Solution

CSV files do not store data types. Data in CSV files is stored as text.

Your best options are:

  1. Store in a serialized or other type-aware format (pickle, HDF5) if this is appropriate for your use case.
  2. Use the parse_dates argument of pd.read_csv, e.g. df = pd.read_csv(filename, sep=',', parse_dates=['Date']). See pd.read_csv documentation for more details.

The second option is just a workaround. It will convert text back to datetime when you read the data into a dataframe.



Answered By - jpp
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing