我在将RangeIndex转换为DatetimeIndex时遇到麻烦。当前,名为“ pred_ci”的数据如下:
lower TRESY10y upper TRESY10y
5685 0.596048 0.823646
5686 0.596048 0.823646
5687 0.636040 0.863637
5688 0.626042 0.853639
5689 0.596048 0.823646
5690 0.586050 0.813648
5691 0.538780 0.860617
5692 0.502485 0.896610
5693 0.471873 0.926921
5694 0.444894 0.953598
5695 0.420497 0.977694
我希望将5685、5686 ...,5695替换为2020-06-15至2020-06-25日期。我尝试了以下代码:
pred = pd.DatetimeIndex(pred_ci, start="2020-06-15",end="2020-06-25" )
但是我遇到了以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~/opt/anaconda3/envs/tsa_course/lib/python3.7/site-packages/pandas/core/tools/datetimes.py in _convert_listlike(arg, box, format, name, tz)
376 try:
--> 377 values, tz = conversion.datetime_to_datetime64(arg)
378 return DatetimeIndex._simple_new(values, name=name, tz=tz)
pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.datetime_to_datetime64()
TypeError: Unrecognized value type: <class 'str'>
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-188-cc6d5175089c> in <module>
----> 1 pred = pd.DatetimeIndex(pred_ci, start="2020-06-15",end="2020-06-25" )
2 pred
~/opt/anaconda3/envs/tsa_course/lib/python3.7/site-packages/pandas/core/indexes/datetimes.py in __new__(cls, data, freq, start, end, periods, tz, normalize, closed, ambiguous, dayfirst, yearfirst, dtype, copy, name, verify_integrity)
397 is_integer_dtype(data)):
398 data = tools.to_datetime(data, dayfirst=dayfirst,
--> 399 yearfirst=yearfirst)
400
401 if issubclass(data.dtype.type, np.datetime64) or is_datetimetz(data):
~/opt/anaconda3/envs/tsa_course/lib/python3.7/site-packages/pandas/core/tools/datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, box, format, exact, unit, infer_datetime_format, origin, cache)
465 result = _convert_and_box_cache(arg, cache_array, box, errors)
466 else:
--> 467 result = _convert_listlike(arg, box, format)
468 else:
469 result = _convert_listlike(np.array([arg]), box, format)[0]
~/opt/anaconda3/envs/tsa_course/lib/python3.7/site-packages/pandas/core/tools/datetimes.py in _convert_listlike(arg, box, format, name, tz)
378 return DatetimeIndex._simple_new(values, name=name, tz=tz)
379 except (ValueError, TypeError):
--> 380 raise e
381
382 if arg is None:
~/opt/anaconda3/envs/tsa_course/lib/python3.7/site-packages/pandas/core/tools/datetimes.py in _convert_listlike(arg, box, format, name, tz)
366 dayfirst=dayfirst,
367 yearfirst=yearfirst,
--> 368 require_iso8601=require_iso8601
369 )
370
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/parsing.pyx in pandas._libs.tslibs.parsing.parse_datetime_string()
~/opt/anaconda3/envs/tsa_course/lib/python3.7/site-packages/dateutil/parser/_parser.py in parse(timestr, parserinfo, **kwargs)
1354 return parser(parserinfo).parse(timestr, **kwargs)
1355 else:
-> 1356 return DEFAULTPARSER.parse(timestr, **kwargs)
1357
1358
~/opt/anaconda3/envs/tsa_course/lib/python3.7/site-packages/dateutil/parser/_parser.py in parse(self, timestr, default, ignoretz, tzinfos, **kwargs)
646
647 if res is None:
--> 648 raise ValueError("Unknown string format:", timestr)
649
650 if len(res) == 0:
ValueError: ('Unknown string format:', 'lower TRESY10y')
我不确定如何将当前索引替换为上述范围的日期。请帮忙。
答案 0 :(得分:2)
要获取DateTimeIndex
,请使用date_range
:
pred_ci.index = pd.date_range("2020-06-15", "2020-06-25", periods=len(pred_ci.index))
答案 1 :(得分:2)
使用此:
pred_ci.index = pd.date_range(start="2020-06-15",end="2020-06-25" )
print(pred_ci)
输出:
lower TRESY10y upper TRESY10y
2020-06-15 0.596048 0.823646
2020-06-16 0.596048 0.823646
2020-06-17 0.636040 0.863637
2020-06-18 0.626042 0.853639
2020-06-19 0.596048 0.823646
2020-06-20 0.586050 0.813648
2020-06-21 0.538780 0.860617
2020-06-22 0.502485 0.896610
2020-06-23 0.471873 0.926921
2020-06-24 0.444894 0.953598
2020-06-25 0.420497 0.977694