1

I'm just wondering if I can do something like:

df.loc['1990':'2000']

by doing something like:

my_slice = '1990':'2000'
df.loc[my_slice]

What I've written doesn't work, but is there something similar that does?

cjm2671
  • 18,348
  • 31
  • 102
  • 161

1 Answers1

1

Yes, but you don't write slices like that. You write slice('1900', '2000', None) instead.

skyking
  • 13,817
  • 1
  • 35
  • 57
  • 1
    What's the 'None' part? (can't find slice in the docs) Thanks! – cjm2671 Jul 04 '16 at 07:34
  • @cjm2671 that's the `step`, just like the `:` notation - `None` is the default, so redundant. As for the docs: https://docs.python.org/2/library/functions.html#slice – jonrsharpe Jul 04 '16 at 07:37