0

How can I slice a numpy array with pandas timestamp?

dates = pd.date_range('1/1/2000', periods=18)
narray_dates=np.array(dates)

How can I slice the narray_dates with beginDate and endDate, which are pandas.Timestamp and can be without the narray_dates?

or is there similar way to solve the problem without numpy array?

Lei You
  • 11
  • 1
    Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael May 25 '17 at 05:54
  • What is your desired output? – Riley Hun May 25 '17 at 06:31

1 Answers1

0

As with many things pandas, there's a method for that.

slicer = dates.slice_indexer(start, end)
slice = dates[slicer]

Example

In [30]: dates = pd.date_range('1/1/2000', periods=18)

In [31]: a = dates.slice_indexer(pd.datetime(2000,1,3),pd.datetime(2000,1,9))

In [32]: dates[a]
Out[32]: 
DatetimeIndex(['2000-01-03', '2000-01-04', '2000-01-05', '2000-01-06',
               '2000-01-07', '2000-01-08', '2000-01-09'],
              dtype='datetime64[ns]', freq='D')

see here: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DatetimeIndex.slice_indexer.html#pandas.DatetimeIndex.slice_indexer