4

I have a pandas DataFrame portfolio whose keys are dates. I'm trying to access multiple rows through

print(portfolio.loc[['2007-02-26','2008-02-06'],:]),

but am getting an error

KeyError: "None of [Index(['2007-02-26', '2008-02-06'], dtype='object', name='Date')] are in the [index]"

However, print(portfolio.loc['2007-02-26',:]) successfully returns

holdings      1094.6124
pos_diff       100.0000
cash         98905.3876
total       100000.0000
returns          0.0000
Name: 2007-02-26 00:00:00, dtype: float64

Isn't this a valid format--> df.loc[['key1', 'key2', 'key3'], 'Column1]?

Marco Deicas
  • 53
  • 1
  • 3
  • 3
    Does this answer your question? [How are iloc, ix and loc different?](https://stackoverflow.com/questions/31593201/how-are-iloc-ix-and-loc-different) – sushanth Jun 14 '20 at 04:15
  • 1
    Whenever I've done this I use a tuple, not a list, of row labels. Maybe that's the issue? There is a `df[(row1, row2), column]` example in the docs for loc: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.loc.html. There is also an example using a list, but the single column name is also in a list in that case...not sure if it's important. – alkasm Jun 14 '20 at 04:19
  • @Sushanth According to the post df.loc[[] ] is a valid expression, but it doesn't address my error – Marco Deicas Jun 14 '20 at 04:27
  • 1
    Could you please provide sample input? – Roy2012 Jun 14 '20 at 04:45
  • You should provide `df.head(7)` in your question or maybe `df.head(7).to_dict()`. – wwii Jun 14 '20 at 12:53

1 Answers1

3

It seems that the issue is with type conversion from strings to timestamps. The solution is, therefore, to explicitly convert the set of labels to DateTime before passing them to loc:

df = pd.DataFrame({"a" : range(5)}, index = pd.date_range("2020-01-01", freq="1D", periods=5))
print(df) 

==> 
            a
2020-01-01  0
2020-01-02  1
2020-01-03  2
2020-01-04  3
2020-01-05  4

try:
    df.loc[["2020-01-01", "2020-01-02"], :]    
except Exception as e:
    print (e) 

 ==>
"None of [Index(['2020-01-01', '2020-01-02'], dtype='object')] are in the [index]"

# But - if you convert the labels to datetime before calling loc, 
# it works fine. 
df.loc[pd.to_datetime(["2020-01-01", "2020-01-02"]), :]

===>
            a
2020-01-01  0
2020-01-02  1
Roy2012
  • 11,755
  • 2
  • 22
  • 35