I have a DataFrame with multiple columns, df
. One of those columns is named Date
and whose items are datetime
objects.
Given a date, datequery
, I want to return the row that is closest to that date.
Going off of this previous answer, I thought of using the following code:
result = min(df.iterrows(), key=lambda x: abs(x['Date'] - datequery))
The way I mentally walk through the code is thusly:
- I give the
min
function an iterable object that iterates over each row since I want the output of the function to be the row min
then evaluates the minimum value of thekeyfunction
and outputs the corresponding item from the iterable. So it would output the row (item) corresponding to the lowest value given by thekeyfunction
Instead of doing this, I'm greeted by the following error:
In [21]: min(df.iterrows(), key=lambda x: abs(x['Date'] - datequery))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
c:/Random/Path/script.py in <module>()
----> 1 min(df.iterrows(), key=lambda x: abs(x['Date'] - datequery))
c:/Random/Path/script.py in <lambda>(x)
----> 1 min(df.iterrows(), key=lambda x: abs(x['Date'] - datequery))
TypeError: tuple indices must be integers or slices, not str
I'm well aware of the fact that tuple indices can't be a string. So my question is three fold:
- What tuple is it talking about? Because there are no tuples being used (unless it's something inside
datetime
) - Where is this string coming from. Again, the only string in the workspace is
'Date'
, but that should only be for accessing the 'Date' column, not as indices for a tuple. - How do I do this?