Hugo
Hugo

Reputation: 1688

How do I convert objects to datetime in a Pandas dataframe?

I have the following code to import from a CSV file

    data = pd.read_csv(("dados_meteo.csv"),\
               names=['POM','DTM','RNF','WET','HMD','TMP','DEW','INF'],\
               parse_dates = ['DTM'])

then

 data.dtypes

returns

POM     object
DTM     object
RNF    float64
WET    float64
HMD    float64
TMP    float64
DEW    float64
INF      int64
dtype: object

After using

data['DTM'] = data['DTM'].astype('datetime64[ns]')

The DTM keeps the same type. Could you help me?

Thank you

Upvotes: 3

Views: 15048

Answers (2)

Ozkan Serttas
Ozkan Serttas

Reputation: 1007

Once you created your data frame you could convert your object to date time type. Here is the way I did

pd.to_datetime(data.DTM, errors = 'ignore')

If you do not have missing values in your raw data you might not need to use errors = 'ignore'

Hope that helps!

Upvotes: 1

Chirag
Chirag

Reputation: 1508

Check your CSV file date column and make sure it is set to as date type ( or else select column=> right click =>Format cells=>Under category select Date=>and select date format)

then

data =pd.read_csv("dados_meteo.csv",parse_dates=['date-coumn-name-here'])

Upvotes: 1

Related Questions