Data Visualization - Error

infy_close.index = pd.to_datetime(infy_close.index)

gives an error as follows;

TypeError Traceback (most recent call last)
D:\Programs\Anaconda\lib\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)
in ()
4 infy_close.set_index(“Date”, inplace=True) # Set Date as the index
5
----> 6 infy_close.index = pd.to_datetime(infy_close.index)
7
8 infy_close

Could you please help me solve this.

Hi Subrahmanya,



Kindly post top and bottom 5 rows of your dataframe to be able to help you better.



Thanks,

Ashish

Here's the output of head();

  Symbol Series Date Prev Close Open Price High Price Low Price Last Price Close Price Average Price Total Traded Quantity Turnover No. of Trades Deliverable Qty % Dly Qt to Traded Qty
0 INFY EQ 01-Jan-16 1105.4 1100 1108.95 1093.2 1103 1105.25 1103.13 903275 996433258.7 18982 331554 36.71
1 INFY EQ 04-Jan-16 1105.25 1099.95 1102.45 1076.05 1078.5 1078.9 1087.82 1987681 2162230784 53877 1247329 62.75
2 INFY EQ 05-Jan-16 1078.9 1085.5 1085.5 1062.25 1073 1074.05 1073.17 2474893 2655975461 62085 1566206 63.28
3 INFY EQ 06-Jan-16 1074.05 1074.65 1074.65 1058.9 1068.5 1069.35 1067.55 2794164 2982910238 76128 1985200 71.05
4 INFY EQ 07-Jan-16 1069.35 1060.05 1067.9 1047.9 1052.55 1050.8 1057.5 2647044 2799255720 79845 1650344 62.35

Here's the output of tail();

  Symbol Series Date Prev Close Open Price High Price Low Price Last Price Close Price Average Price Total Traded Quantity Turnover No. of Trades Deliverable Qty % Dly Qt to Traded Qty
739 INFY EQ 24-Dec-18 646.2 657 658.8 647.6 648.5 649 652.59 8379825 5468562526 140084 3230709 38.55
740 INFY EQ 26-Dec-18 649 647 647 637.35 643.9 644.05 641.76 6178552 3965160752 129003 3192480 51.67
741 INFY EQ 27-Dec-18 644.05 652.75 663.25 649 656.35 656.8 658.16 15333902 10092126951 182988 7084546 46.2
742 INFY EQ 28-Dec-18 656.8 663.35 663.5 653.3 656.1 656.95 658.29 4736598 3118076702 105527 2225579 46.99
743 INFY EQ 31-Dec-18 656.95 660 662 655.8 659.6 658.95 659.27 3373319 2223919550 65442 1517551 44.99

There was a blank row at the end which I removed & tried, however I am still getting the same error.

The code snippet is already shown in my first message, please help.

Hi Subrahmanya,



The data you have posted doesn't suggest that the current index of you dataframe is in a format which can be interpreted by the pd.to_datetime as datetime. 



I think the current index is simply the first unnamed column containing numeric values, which can't be converted to a datetime index.



You may try the code below before the infy_close.index = pd.to_datetime(infy_close.index) 



infy_close.set_index("Date",drop=True,inplace=True)



Thanks,

Ashish Garg