如何使用pandas.to_datetime将jalali日期字符串转换为python日期时间对象

2024-04-29 03:46:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我在使用pandas.to_datetime将jalali日期字符串转换为python日期时间对象时遇到一些问题 例如,当我运行以下简单代码时:

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

我得到了这个错误:

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


Tags: toinformatpandasdatetimerequirelibspyx
2条回答

由于熊猫以纳秒分辨率表示时间戳,因此可以使用64位整数表示的时间跨度被限制为大约584年。 Out of bounds nanosecond timestamp

在日期时间限制中,选中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')

可能的解决方案是将值转换为每日时段,more info

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

或者使用纯python:

from datetime import datetime

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

带列的解决方案:

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]

如果使用另一种方法获取对象,而不是日期时间:

from datetime import datetime

df['dates'] = df['dates'].apply(lambda x: datetime.strptime(x, '%Y/%m/%d'))
print (df['dates'].dtype)
object

相关问题 更多 >