Pandas read_csv not recognizing ISO8601 as datetime dtype

Peet Whittaker

Currently I am using pandas to read a csv file into a DataFrame, using the first column as the index. The first column is in ISO 8601 format, so according to the documentation for read_csv, it should be recognized as a datetime:

In [1]: import pandas as pd

In [2]: df = pd.read_csv('data.csv', index_col=0)

In [3]: print df.head()
                        U     V     Z    Ubar    Udir
2014-11-01 00:00:00  0.73 -0.81  0.46  1.0904  317.97
2014-11-01 01:00:00  1.26 -1.50  0.32  1.9590  319.97
2014-11-01 02:00:00  1.50 -1.80  0.13  2.3431  320.19
2014-11-01 03:00:00  1.39 -1.65  0.03  2.1575  319.89
2014-11-01 04:00:00  0.94 -1.08 -0.03  1.4318  318.96

However, when querying the index dtype, it returns 'object':

In [4]: print df.index.dtype
object

I then have to manually convert it to datetime dtype:

In [5]: df.index = pd.to_datetime(df.index)

In [6]: print df.index.dtype
datetime64[ns]

Is there any way to automatically have the index set to datetime dtype when calling read_csv()?

Tautvydas

read_csv documentation describes parse_dates parameter:

parse_dates : boolean or list of ints or names or list of lists or dict, default False
- boolean. If True -> try parsing the index.
- list of ints or names. e.g. If [1, 2, 3] -> try parsing columns 1, 2, 3 each as a separate date column.
- list of lists. e.g. If [[1, 3]] -> combine columns 1 and 3 and parse as a single date column.
- dict, e.g. {‘foo’ : [1, 3]} -> parse columns 1, 3 as date and call result ‘foo’
Note: A fast-path exists for iso8601-formatted dates.

Since you want to parse index you can use:

 import pandas as pd
 df = pd.read_csv('data.csv', index_col=0, parse_dates=True)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Pandas read_csv dtype read all columns but few as string

From Dev

Pandas read_csv dtype read all columns but few as string

From Java

Pandas read_csv low_memory and dtype options

From Dev

Pandas read_csv dtype specify all columns but one

From Dev

pandas read_csv column dtype is set to decimal but converts to string

From Dev

Pandas read_csv dtype specify all columns but one

From Dev

pandas read_csv column dtype is set to decimal but converts to string

From Dev

SQLite datetime comparison with 'now' in ISO8601

From Dev

Ruby check if datetime is a iso8601 and saving

From Dev

pandas: read_csv how to force bool data to dtype bool instead of object

From Dev

ISO8601 to DateTime with time zone information preserved

From Dev

ISO8601 DateTime String to be displayed in local users timezone

From Dev

pandas to_csv and then read_csv results to numpy.datetime64 messed up due to utc

From Dev

Questions about read_csv and str dtype

From Dev

StringIO and pandas read_csv

From Dev

how to specify the datetime format in read_csv

From Dev

Converting a date, time and offset into an ISO8601 DateTime using moment.js and moment timezone

From Dev

PHP $date->format(DateTime::ISO8601) returning different timezone offset

From Dev

What's the difference between dtype and converters in pandas.read_csv?

From Dev

Convert DateTime to ISO 8601

From Dev

ISO 8601 datetime now

From Dev

Convert DateTime to ISO 8601

From Dev

Pandas DatetimeIndex indexing dtype: datetime64 vs Timestamp

From Dev

using pandas read_csv with missing data

From Dev

How to speed up pandas read_csv?

From Dev

pandas read_csv error on windows

From Java

Pandas read_csv from url

From Dev

pandas - read_csv with missing values in headline

From Dev

Pandas Dataframe memory read_csv

Related Related

  1. 1

    Pandas read_csv dtype read all columns but few as string

  2. 2

    Pandas read_csv dtype read all columns but few as string

  3. 3

    Pandas read_csv low_memory and dtype options

  4. 4

    Pandas read_csv dtype specify all columns but one

  5. 5

    pandas read_csv column dtype is set to decimal but converts to string

  6. 6

    Pandas read_csv dtype specify all columns but one

  7. 7

    pandas read_csv column dtype is set to decimal but converts to string

  8. 8

    SQLite datetime comparison with 'now' in ISO8601

  9. 9

    Ruby check if datetime is a iso8601 and saving

  10. 10

    pandas: read_csv how to force bool data to dtype bool instead of object

  11. 11

    ISO8601 to DateTime with time zone information preserved

  12. 12

    ISO8601 DateTime String to be displayed in local users timezone

  13. 13

    pandas to_csv and then read_csv results to numpy.datetime64 messed up due to utc

  14. 14

    Questions about read_csv and str dtype

  15. 15

    StringIO and pandas read_csv

  16. 16

    how to specify the datetime format in read_csv

  17. 17

    Converting a date, time and offset into an ISO8601 DateTime using moment.js and moment timezone

  18. 18

    PHP $date->format(DateTime::ISO8601) returning different timezone offset

  19. 19

    What's the difference between dtype and converters in pandas.read_csv?

  20. 20

    Convert DateTime to ISO 8601

  21. 21

    ISO 8601 datetime now

  22. 22

    Convert DateTime to ISO 8601

  23. 23

    Pandas DatetimeIndex indexing dtype: datetime64 vs Timestamp

  24. 24

    using pandas read_csv with missing data

  25. 25

    How to speed up pandas read_csv?

  26. 26

    pandas read_csv error on windows

  27. 27

    Pandas read_csv from url

  28. 28

    pandas - read_csv with missing values in headline

  29. 29

    Pandas Dataframe memory read_csv

HotTag

Archive