2

Given a date, I'm using pandas date_range to generate additional 30 dates:

import pandas as pd
from datetime import timedelta
pd.date_range(startdate, startdate + timedelta(days=30))

Out of these 30 dates, how can I randomly select 10 dates in order starting from date in first position and ending with date in last position?

2 Answers 2

5
  • use np.random.choice to choose specified number of items from a given set of choices.
  • In order to guarantee that the first and last dates are preserved, I pull them out explicitly and choose 8 more dates at random.
  • I then pass them back to pd.to_datetime and sort_values to ensure they stay in order.

dates = pd.date_range('2011-04-01', periods=30, freq='D')
random_dates = pd.to_datetime(
    np.concatenate([
            np.random.choice(dates[1:-1], size=8, replace=False),
            dates[[0, -1]]
        ])
    ).sort_values() 

random_dates

DatetimeIndex(['2011-04-01', '2011-04-02', '2011-04-03', '2011-04-13',
               '2011-04-14', '2011-04-21', '2011-04-22', '2011-04-26',
               '2011-04-27', '2011-04-30'],
              dtype='datetime64[ns]', freq=None)
Sign up to request clarification or add additional context in comments.

Comments

2

You can use numpy.random.choice with replace=False if is not necessary explicitly get first and last value (if yes use another answer):

a = pd.date_range('2011-04-01', periods=30, freq='D')
print (pd.to_datetime(np.sort(np.random.choice(a, size=10, replace=False))))
DatetimeIndex(['2011-04-01', '2011-04-03', '2011-04-05', '2011-04-09',
               '2011-04-12', '2011-04-17', '2011-04-22', '2011-04-24',
               '2011-04-29', '2011-04-30'],
              dtype='datetime64[ns]', freq=None)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.