`to_datetime` limit or misuse? ValueError : Doesn't match format specified

Aetos

I can't seem to convert a series containing date strings into a dtype of datetime64. The following code reproduces the error:

import pandas as pd

gud_date_s = pd.Series(["2019/12/31 00:00:00.0"]*100)
gud_date_s2 = pd.Series(["2261/12/31 00:00:00.0"]*100)
bad_date_s = pd.Series(["9999/12/31 00:00:00.0"]*100)
bad_date_s2 = pd.Series(["2262/12/31 00:00:00.0"]*100)


gd1 = pd.to_datetime(gud_date_s, format="%Y/%m/%d", yearfirst=True).dt.date # Correct
gd2 = pd.to_datetime(gud_date_s2 , format="%Y/%m/%d", yearfirst=True).dt.date # Correct
bd1 = pd.to_datetime(bad_date_s, format="%Y/%m/%d", yearfirst=True).dt.date 
#Returns {ValueError}time data 9999/12/31 00:00:00.0 doesn't match format specified.
bd2 = pd.to_datetime(bad_date_s2 , format="%Y/%m/%d", yearfirst=True).dt.date
#Returns {ValueError}time data 2262/12/31 00:00:00.0 doesn't match format specified.

So the threshold of accepted years seems to be 2261. Why? How do I fix this?

N.B: dates such as 9999/12/31 are relevant, Therefore I would like to keep them as-is.

Cheers

jezrael

Here is not valid value year 9999, so is necessary errors='coerce' for convert to NaT:

bd1 = pd.to_datetime(bad_date_s, format="%Y/%m/%d", yearfirst=True, errors='coerce').dt.date

And here is raised error, because limit, year is correct, but maximum month and day is only 11th April:

Unfortunately here error should be more clear.

bd2 = pd.to_datetime(bad_date_s2 , format="%Y/%m/%d", yearfirst=True, errors='coerce').dt.date

print (pd.Timestamp.max)
2262-04-11 23:47:16.8547758

For working with datetimes it raise error:

from datetime import datetime

d = datetime(year=9999, month=12, day=31)
bd1 = pd.to_datetime(bad_date_s, format="%Y/%m/%d", yearfirst=True, errors='coerce').dt.date.fillna(d)
print (bd1)

OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 9999-12-31 00:00:00

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

LIMIT doesn't work in datetime comparison

分類Dev

Converting date and time from String format to Python datetime object: ValueError: time data '... p.m.' does not match format '... %p'

分類Dev

ValueError: non-broadcastable output operand with shape (3,1) doesn't match the broadcast shape (3,4)

分類Dev

Pandas to_datetime ValueError:不明な文字列形式

分類Dev

PHP preg_match doesn't match

分類Dev

ValueError: time data '2018-12-22T00:41:30.926932Z' does not match format '%Y-%m-%d %H:%M:%S'

分類Dev

Why doesn't this to_date work, when the results have been filtered to match my date format (Oracle SQL)

分類Dev

XML to XSLT pattern doesn't match

分類Dev

Why doesn't ::1 match 127.0.0.1?

分類Dev

Java dot doesn't match 'any character'

分類Dev

Fill color doesn't match outline color

分類Dev

Regex doesn't match with the lines in txt file

分類Dev

Can't format datetime in carbon

分類Dev

String.Format() doesn't work, but string.Format() does

分類Dev

Altering character limit on Heroku PostGRES doesn't change column in database

分類Dev

Why doesn't Spring's PathMatchingResourcePatternResolver match "*" with nothing?

分類Dev

Why doesn't this regex capture group repeat for each match?

分類Dev

Certificate for <localhost> doesn't match any of the subject alternative names

分類Dev

Elasticsearch doesn't return results on a multi match query

分類Dev

junit.framework.AssertionFailedError: doesn't match the selected view

分類Dev

Moq verify method signature sees invocations but doesn't match on them?

分類Dev

tSQLt AssertEqualsTable - unexpected results when table schema doesn't match

分類Dev

python re.findall doesn't match duplicates

分類Dev

Output inside function scope doesn't match that of it's parent

分類Dev

Why doesn't LOOKUP match the first element in an array?

分類Dev

preg_match_all doesn't return the expected value

分類Dev

man page of swapon doesn't match with what I am seeing

分類Dev

QEMU: CPU topology doesn‘t match maximum vcpu count

分類Dev

My time data "does not match format". How to properly format my datetime?

Related 関連記事

  1. 1

    LIMIT doesn't work in datetime comparison

  2. 2

    Converting date and time from String format to Python datetime object: ValueError: time data '... p.m.' does not match format '... %p'

  3. 3

    ValueError: non-broadcastable output operand with shape (3,1) doesn't match the broadcast shape (3,4)

  4. 4

    Pandas to_datetime ValueError:不明な文字列形式

  5. 5

    PHP preg_match doesn't match

  6. 6

    ValueError: time data '2018-12-22T00:41:30.926932Z' does not match format '%Y-%m-%d %H:%M:%S'

  7. 7

    Why doesn't this to_date work, when the results have been filtered to match my date format (Oracle SQL)

  8. 8

    XML to XSLT pattern doesn't match

  9. 9

    Why doesn't ::1 match 127.0.0.1?

  10. 10

    Java dot doesn't match 'any character'

  11. 11

    Fill color doesn't match outline color

  12. 12

    Regex doesn't match with the lines in txt file

  13. 13

    Can't format datetime in carbon

  14. 14

    String.Format() doesn't work, but string.Format() does

  15. 15

    Altering character limit on Heroku PostGRES doesn't change column in database

  16. 16

    Why doesn't Spring's PathMatchingResourcePatternResolver match "*" with nothing?

  17. 17

    Why doesn't this regex capture group repeat for each match?

  18. 18

    Certificate for <localhost> doesn't match any of the subject alternative names

  19. 19

    Elasticsearch doesn't return results on a multi match query

  20. 20

    junit.framework.AssertionFailedError: doesn't match the selected view

  21. 21

    Moq verify method signature sees invocations but doesn't match on them?

  22. 22

    tSQLt AssertEqualsTable - unexpected results when table schema doesn't match

  23. 23

    python re.findall doesn't match duplicates

  24. 24

    Output inside function scope doesn't match that of it's parent

  25. 25

    Why doesn't LOOKUP match the first element in an array?

  26. 26

    preg_match_all doesn't return the expected value

  27. 27

    man page of swapon doesn't match with what I am seeing

  28. 28

    QEMU: CPU topology doesn‘t match maximum vcpu count

  29. 29

    My time data "does not match format". How to properly format my datetime?

ホットタグ

アーカイブ