1

I got some problem with converting jalali date string to python date time object with pandas.to_datetime for example when i run this simple code :

print(pd.to_datetime('1399/05/02',format='%Y/%m/%d'))

I got this error :

TypeError                                 Traceback (most recent call last)
~\anaconda3\envs\tf2\lib\site-packages\pandas\core\arrays\datetimes.py in objects_to_datetime64ns(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object)
   1857         try:
-> 1858             values, tz_parsed = conversion.datetime_to_datetime64(data)
   1859             # If tzaware, these values represent unix timestamps, so we

pandas\_libs\tslibs\conversion.pyx in pandas._libs.tslibs.conversion.datetime_to_datetime64()

TypeError: Unrecognized value type: 

During handling of the above exception, another exception occurred:

OutOfBoundsDatetime                       Traceback (most recent call last)
 in 
      1 #dbs.Irdate = pd.to_datetime(dbs.Irdate,format='%Y/%m/%d',yearfirst = True)
----> 2 print(pd.to_datetime('1399/05/02',format='%Y/%m/%d'))

~\anaconda3\envs\tf2\lib\site-packages\pandas\core\tools\datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)
    754             result = convert_listlike(arg, format)
    755     else:
--> 756         result = convert_listlike(np.array([arg]), format)[0]
    757 
    758     return result

~\anaconda3\envs\tf2\lib\site-packages\pandas\core\tools\datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
    445             errors=errors,
    446             require_iso8601=require_iso8601,
--> 447             allow_object=True,
    448         )
    449 

~\anaconda3\envs\tf2\lib\site-packages\pandas\core\arrays\datetimes.py in objects_to_datetime64ns(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object)
   1861             return values.view("i8"), tz_parsed
   1862         except (ValueError, TypeError):
-> 1863             raise e
   1864 
   1865     if tz_parsed is not None:

~\anaconda3\envs\tf2\lib\site-packages\pandas\core\arrays\datetimes.py in objects_to_datetime64ns(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object)
   1852             dayfirst=dayfirst,
   1853             yearfirst=yearfirst,
-> 1854             require_iso8601=require_iso8601,
   1855         )
   1856     except ValueError as e:

pandas\_libs\tslib.pyx in pandas._libs.tslib.array_to_datetime()

pandas\_libs\tslib.pyx in pandas._libs.tslib.array_to_datetime()

pandas\_libs\tslib.pyx in pandas._libs.tslib.array_to_datetime()

pandas\_libs\tslib.pyx in pandas._libs.tslib.array_to_datetime()

pandas\_libs\tslibs\np_datetime.pyx in pandas._libs.tslibs.np_datetime.check_dts_bounds()

**OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1399-05-02 00:00:00**

so i use this Parameter errors='ignore' for fixing errors but result is unacceptable for example :

print(pd.to_datetime('1399/05/02',format='%Y/%m/%d', errors='ignore'))

result is : 1399/05/02 so you can see nothing change i want some thing like this 1399-05-02 the python date time object please help me

mahdi101
  • 87
  • 2
  • 5

2 Answers2

3

In pandas are limitation for datetimes, check this:

In [92]: pd.Timestamp.min
Out[92]: Timestamp('1677-09-21 00:12:43.145225')

In [93]: pd.Timestamp.max
Out[93]: Timestamp('2262-04-11 23:47:16.854775807')

Possible solution is convert values to daily periods, more info:

p = pd.Period('1399/05/02')
print (p)
1399-05-02

Or use pure python:

from datetime import datetime

d = datetime.strptime('1399/05/02', '%Y/%m/%d')
print (d)
1399-05-02 00:00:00

Solutions with column:

df = pd.DataFrame({'dates':['1399/05/02','1999/05/02']})

df['dates'] = df['dates'].apply(pd.Period)
print (df)
        dates
0  1399-05-02
1  1999-05-02

print (df['dates'].dtype)
period[D]

If use another idea get objects, not datetimes:

from datetime import datetime

df['dates'] = df['dates'].apply(lambda x: datetime.strptime(x, '%Y/%m/%d'))
print (df['dates'].dtype)
object
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

Since pandas represent timestamps in nanosecond resolution, the timespan that can be represented using a 64-bit integer is limited to approximately 584 years. Out of bounds nanosecond timestamp

Hadi
  • 11
  • 5