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)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

パンダread_csvがISO8601を日時dtypeとして認識しない

分類Dev

JodaTime DateTime、ISO8601 GMT日付形式

分類Dev

Java Jsonb deserializing UTC datetime in ISO8601

分類Dev

Pandas read_csv()によって読み込まれたdtypeの辞書を取得します

分類Dev

正規表現およびISO8601形式のDateTime

分類Dev

ISO8601のDateTime + TimeZoneおよびマイクロ秒なし

分類Dev

DateTimeをISO8601に変換します

分類Dev

C# Pandas read_csv Equivalent

分類Dev

Pandas read_csv only first comma

分類Dev

Dictionary to CSV with Pandas to_csv and reload it with read_csv

分類Dev

MySQLフィールドDATETIMEはISO8601を切り捨てます

分類Dev

ISO8601期間からDateTimeオブジェクトを取得します

分類Dev

ISO8601フィールドからPythonのDateTimeフィールド

分類Dev

pandas read_csvは、(名前ではなく)列インデックスによって `dtype`を設定します

分類Dev

pandas read_csv列のdtypeは10進数に設定されていますが、文字列に変換されます

分類Dev

Pandas read_csv reads rows wrongfully as dictionaries

分類Dev

Pandas, importing JSON-like file using read_csv

分類Dev

Manually set the keys in Pandas DataFrame built with read_csv

分類Dev

pandas.read_csvのdtype = Noneは何をしますか?

分類Dev

ISO8601形式でDateTimeオブジェクトを解析および生成する方法

分類Dev

ISO8601準拠の文字列をperlのDateTimeオブジェクトに解析する方法は?

分類Dev

OrientDBでISO8601準拠の日付をDateTime形式として設定する方法はありますか?

分類Dev

タイムゾーン付きのISO8601日付をJuliaのDateTimeに変換します

分類Dev

正規表現およびISO8601形式のDateTime2016-10-18T00:00:00Zのみ

分類Dev

ISO8601ローカルユーザーのタイムゾーンに表示されるDateTime文字列

分類Dev

PHP $date->format(DateTime::ISO8601) 異なるタイムゾーンオフセットを返す

分類Dev

ISO 8601DateTime表現

分類Dev

パンダのread_csv low_memoryおよびdtypeオプション

分類Dev

パンダ0.10.1でパンダ.read_csvでdtype float32を指定する

Related 関連記事

  1. 1

    パンダread_csvがISO8601を日時dtypeとして認識しない

  2. 2

    JodaTime DateTime、ISO8601 GMT日付形式

  3. 3

    Java Jsonb deserializing UTC datetime in ISO8601

  4. 4

    Pandas read_csv()によって読み込まれたdtypeの辞書を取得します

  5. 5

    正規表現およびISO8601形式のDateTime

  6. 6

    ISO8601のDateTime + TimeZoneおよびマイクロ秒なし

  7. 7

    DateTimeをISO8601に変換します

  8. 8

    C# Pandas read_csv Equivalent

  9. 9

    Pandas read_csv only first comma

  10. 10

    Dictionary to CSV with Pandas to_csv and reload it with read_csv

  11. 11

    MySQLフィールドDATETIMEはISO8601を切り捨てます

  12. 12

    ISO8601期間からDateTimeオブジェクトを取得します

  13. 13

    ISO8601フィールドからPythonのDateTimeフィールド

  14. 14

    pandas read_csvは、(名前ではなく)列インデックスによって `dtype`を設定します

  15. 15

    pandas read_csv列のdtypeは10進数に設定されていますが、文字列に変換されます

  16. 16

    Pandas read_csv reads rows wrongfully as dictionaries

  17. 17

    Pandas, importing JSON-like file using read_csv

  18. 18

    Manually set the keys in Pandas DataFrame built with read_csv

  19. 19

    pandas.read_csvのdtype = Noneは何をしますか?

  20. 20

    ISO8601形式でDateTimeオブジェクトを解析および生成する方法

  21. 21

    ISO8601準拠の文字列をperlのDateTimeオブジェクトに解析する方法は?

  22. 22

    OrientDBでISO8601準拠の日付をDateTime形式として設定する方法はありますか?

  23. 23

    タイムゾーン付きのISO8601日付をJuliaのDateTimeに変換します

  24. 24

    正規表現およびISO8601形式のDateTime2016-10-18T00:00:00Zのみ

  25. 25

    ISO8601ローカルユーザーのタイムゾーンに表示されるDateTime文字列

  26. 26

    PHP $date->format(DateTime::ISO8601) 異なるタイムゾーンオフセットを返す

  27. 27

    ISO 8601DateTime表現

  28. 28

    パンダのread_csv low_memoryおよびdtypeオプション

  29. 29

    パンダ0.10.1でパンダ.read_csvでdtype float32を指定する

ホットタグ

アーカイブ