0

My column indexes are of type datetime and look like this:

df.columns
Out[142]: Index([2017-01-01, 2017-01-20, 2017-02-08, 2017-02-27, 2017-03-18, 2017-04-06,
       2017-04-25, 2017-05-14, 2017-06-02, 2017-06-21, 2017-07-10, 2017-07-29,
       2017-08-17, 2017-09-05, 2017-09-24, 2017-10-13, 2017-11-01, 2017-11-20,
       2017-12-09, 2017-12-28, 2018-01-16, 2018-02-05, 2018-02-24, 2018-03-15,
       2018-04-03, 2018-04-22, 2018-05-11, 2018-05-30, 2018-06-18, 2018-07-07,
       2018-07-26, 2018-08-14, 2018-09-02, 2018-09-21, 2018-10-10, 2018-10-29,
       2018-11-17, 2018-12-06],
      dtype='object')

I am trying to return the indexes in df which are in between two dates but I can't figure out how to include both conditions.

I'm able to do it one way:

start = datetime.date(2018, 4, 15)
end = datetime.date(2018, 5, 2)

df.columns < end
Out[146]: array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
    True,  True,  True,  True,  True,  True,  True,  True,  True,
    True,  True,  True,  True,  True,  True,  True,  True, False,
   False, False, False, False, False, False, False, False, False,
   False, False])

but when I try both:

start < customer_flag_table.columns < end

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

How would I return the indexes which are between the dates?

rafvasq
  • 1,512
  • 3
  • 18
  • 48

2 Answers2

0

That <> can not be chained

(start < customer_flag_table.columns) &(customer_flag_table.columns < end)
BENY
  • 317,841
  • 20
  • 164
  • 234
0

Since your DataFrame have the datetime as index, you can just use .loc slicing like so:

df.loc(axis=1)['2018-04-15': '2018-05-02']

dates         2018-04-22
some_data     0.050737

Here's another answer that tackles if your datetime is not an index.

r.ook
  • 13,466
  • 2
  • 22
  • 39