DatetimeIndex.get_loc is deprecated

163
March 29, 2022, at 00:00 AM

I updated Pandas to 1.4.0 with yfinance 0.1.70. Previously, I had to stay with Pandas 1.3.5 as Pandas and yfinance did't play well together. These latest versions of Pandas and yfinance now work together, BUT Pandas now gives me this warning:

Future Warning: Passing method to DatetimeIndex.get_loc is deprecated... Use index.get_indexer([item], method=...) instead

I had enough trouble as a novice Python person getting the original get_loc statement to work:

last_week = format((df.index[df.index.get_loc(last_week, method='nearest')]).strftime('%Y-%m-%d'))

This statement allowed me to get a date from the dataframe that I could use further in determining the value associated with that date:

week_value = df.loc[last_week, ans]

Truth be known, I am intimidated in trying to change this statement to be compliant with the new and improved get_indexer function. Can someone help me out please?

Answer 1

Should be pretty simple. Just change get_loc(XXX, ...) to get_indexer([XXX], ...)[0]:

last_week = format((df.index[df.index.get_indexer([last_week], method='nearest')[0]]).strftime('%Y-%m-%d'))
Rent Charter Buses Company
READ ALSO
Can opencv be used with apple’s coremL (Neural Engine)?

Can opencv be used with apple’s coremL (Neural Engine)?

I have a simple opencv script in python running on an M1 Mac to detect objects withIs it possible to use Apple’s coreML to accelerate OpenCV? I've installed TensorFlow-metal (using https://developer

134
Is there a way to return one specific table from a webpage that has multiple tables in Python?

Is there a way to return one specific table from a webpage that has multiple tables in Python?

I'm having trouble returning one particular table (the one titled 'BRN Substantial Shareholders') from this webpage - https://wwwintelligentinvestor

121
Replace other values with np.nan

Replace other values with np.nan

I have a pandas data frame:

126
Python, calculator class definition

Python, calculator class definition

I'm struggling with a practice problem below where I'm confused as to what the definitions would look like

153