Skip to content Skip to sidebar Skip to footer

First Available Date Previous Day/month/year From A Datetime Range

I have a datetime instance with dates (dfDates): 2017-03-01 00:00 2017-03-02 00:00 2017-03-04 00:00 ... For the last day (here: 2017-03-04) I calculate the previous day/month/year

Solution 1:

Not the most efficient, but you can try:

# From your functionday_minus_one = previous_day(dtToday)

# Return LAST element in INDEX of DF FROM START TO DAY_MINUS_ONEactual = df.loc[:day_minus_one].index[-1]

This essentially returns you the last index of a copy of your df, up to and including day_minus_one, if any of the dates exist. This should give you the closest date, or the date itself.

You can try:

# Returns LAST element of INDEX of df from DAY_MINUS_ONE_HUNDRED to DAY_MINUS_ONEactual_better = df.loc[day_minus_one_hundred:day_minus_one].index[-1]

To only look back one_hundred days from minus_one if your dataset is huge, so you don't have to return a huge array just to find one date.

Solution 2:

If I understand correctly, you don't want to actually subtract 1 day, you want to get the previous available day from the list of available dates. If that's the case, then consider this :

available_dates = [
  2017-03-01 00:00,
  2017-03-02 00:00,
  2017-03-04 00:00,
  ...
]

defprevious_day(dtToday):
    today_index = available_dates.index(dtToday)
    return available_dates[today_index-1]

This assumes, of course, that your available_dates is sorted

EDIT:

If you want to be able to subtract month and years, then something a little bit more complex is needed :

# Sorted
available_dates = [
  2017-03-01 00:00,
  2017-03-02 00:00,
  2017-03-04 00:00,
  ...
]

subtract_from_date(date, day=None, month=None, year=None):
  # check if it's day/month/yeah
  ...
  # do the actual subtraction and store it in substracted_date
  ...
  # get the closest datefor index, date inenumerate(available_dates):
     if date > substracted_date:
       return available_dates[index-1]

Solution 3:

I solved it like this:

  • dtToday = the reference date
  • dtDates = a datetime sequence of the available dates
  • nbOffset = the number of days/months/years we want to go back

Code:

defprevious_day(dtToday, dtDates, nbOffset):
    prevx   = dtToday - pd.DateOffset(days=nbOffset)
    return test_day_in(prevx, dtDates)

defprevious_month(dtToday, dtDates, nbOffset):
    prevx = dtToday - pd.DateOffset(months=nbOffset)
    return test_day_in(prevx, dtDates)

defprevious_year(dtToday, dtDates, nbOffset):
    prevx = dtToday - pd.DateOffset(years=nbOffset)
    return test_day_in(prevx, dtDates)

deftest_day_in(dtTest, dtDates):
    if dtTest in dtDates:
        return dtTest
    else:
        return tryNextDay(dtTest, dtDates)

deftryNextDay(dtTest, dtDates):

    # if not outside the boundif (dtTest < dtDates.min()):
        return dtDates.min()

    # check if next day existif (dtTest + pd.DateOffset(days=1) <= dtDates.max()):
        return previous_day(dtTest + pd.DateOffset(days=2), dtDates, 1) # 2-1else:
        print('warning, tryNextDay triggered')
        # should not be triggered, it should take by default the dtDates.min() if far outside rangereturn dtTest

Post a Comment for "First Available Date Previous Day/month/year From A Datetime Range"