0

I am having trouble converting a RangeIndex into a DatetimeIndex. Currently, the data called "pred_ci" looks as follows:

       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

where I would like that the 5685, 5686..., 5695 be replaced with dates from 2020-06-15 to 2020-06-25. I attempted the following code:

pred = pd.DatetimeIndex(pred_ci, start="2020-06-15",end="2020-06-25" ) 

But I got the following error:

---------------------------------------------------------------------------
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')

I am not sure how to replace the current index into dates with the above range. Please help.

2 Answers 2

2

to get a DateTimeIndex, use a date_range:

pred_ci.index = pd.date_range("2020-06-15", "2020-06-25", periods=len(pred_ci.index))
Sign up to request clarification or add additional context in comments.

Comments

2

Use this:

pred_ci.index = pd.date_range(start="2020-06-15",end="2020-06-25" )
print(pred_ci)

Output:

            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

1 Comment

Thanks @Balaji Ambresh :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.