pd.to_datetime() does not work in windows terminal

50 Views Asked by At

I have been running python scripts in windows power shell for quite some time.

All of a sudden this morning I get this error message:

 line 3802, in get_loc
    return self._engine.get_loc(casted_key)

  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 165, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 5745, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 5753, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'last trade time'

what is strange is that I can run the code 3 different ways:

  1. Integrated Terminal - python test_blah.py (works fine).
  2. Windows power shell - navigate to folder, then python test_blah.py (works fine).
  3. Windows power shell - python "the full path/test_blah.py" (throws error).

the full path looks like this:

python "G:\My Drive\darren\python\test_blah.py"

So I have created a min reproducible example here to show the above:

import pandas as pd

print('pandas version:' , pd.__version__)

def some_function():
    df = pd.read_csv('basic_GBP_stats.csv')
    print(df)  
    print('got here 01')

    try:
        df['time'] = pd.to_datetime(df['last trade time'])
        print('got here 02')
    
    except Exception as e:
        print('exception found:', e)


some_function()

The contents of the (example) CSV file are this:

       Unnamed: 0  symbol  num trades  ...  wavg sell px  wavg ratio %      last trade time
0               0  ADAGBP         309  ...      0.261606      0.235289  2022-12-13 21:02:32
1               1  BTCGBP        3949  ...  14127.293571    -10.086920  2022-12-13 20:25:34
2               2  ETHGBP        1349  ...   1025.139765      0.183433  2022-12-13 20:18:14
3               3  SOLGBP        1467  ...     11.241261      0.193498  2022-12-13 17:42:31
4               4  XRPGBP        1005  ...      0.314160      0.333005  2022-12-13 22:13:14
...           ...     ...         ...  ...           ...           ...                  ...
15295           1  BTCGBP        6215  ...  16612.732328     -3.116873  2023-04-24 08:50:25
15296           2  ETHGBP        2075  ...   1139.706164      0.152275  2023-04-24 07:05:07
15297           3  SOLGBP        2366  ...     12.127936      2.285372  2023-04-23 15:48:48
15298           4  XRPGBP        1451  ...      0.315087      0.836744  2023-04-24 07:26:02
15299           5  BNBGBP         405  ...    219.697751      0.476855  2023-04-22 12:35:53

The version of pandas that is used: 1.5.3.

What I notice is that the df['time'] = pd.to_datetime(df['last trade time']) for some reason produces a malformed dataframe (from method 3 but works fine with methods 1 and 2), which is the reason for the error when using method 3 above.

But I don't know why or how to fix this...

0

There are 0 best solutions below