Calculate Last Friday of Month in Pandas

前端 未结 3 1494
野性不改
野性不改 2021-01-15 06:34

I\'ve written this function to get the last Thursday of the month

def last_thurs_date(date):
    month=date.dt.month
    year=date.dt.year
    
    cal = cale         


        
相关标签:
3条回答
  • 2021-01-15 06:47

    Jpp already added the solution, but just to add a slightly more readable formatted string - see this awesome website.

    import calendar
    def last_thurs_date(date):
        year, month = date.year, date.month
        cal = calendar.monthcalendar(year, month)
        # the last (4th week -> row) thursday (4th day -> column) of the calendar
        # except when 0, then take the 3rd week (February exception)
        last_thurs_date =  cal[4][4] if cal[4][4] > 0 else cal[3][4] 
        return f'{year}-{month:02d}-{last_thurs_date}'
    

    Also added a bit of logic - e.g. you got 2019-02-0 as February doesn't have 4 full weeks.

    0 讨论(0)
  • 2021-01-15 06:51

    Scalar datetime objects don't have a dt accessor, series do: see pd.Series.dt. If you remove this, your function works fine. The key is understanding that pd.Series.apply passes scalars to your custom function via a loop, not an entire series.

    def last_thurs_date(date):
        month = date.month
        year = date.year
    
        cal = calendar.monthcalendar(year, month)
        last_thurs_date = cal[4][4]
        if month < 10:
            thurday_date = str(year)+'-0'+ str(month)+'-' + str(last_thurs_date)
        else:
            thurday_date = str(year) + '-' + str(month) + '-' + str(last_thurs_date)
        return thurday_date
    

    You can rewrite your logic more succinctly via f-strings (Python 3.6+) and a ternary statement:

    def last_thurs_date(date):
        month = date.month
        year = date.year
        last_thurs_date = calendar.monthcalendar(year, month)[4][4]
        return f'{year}{"-0" if month < 10 else "-"}{month}-{last_thurs_date}'
    
    0 讨论(0)
  • 2021-01-15 07:04
    • This question answer Calculate Last Friday of Month in Pandas
      • This can be modified by selecting the appropriate day of the week, here freq='W-FRI'
    • I think the easiest way is to create a pandas.DataFrame using pandas.date_range and specifying freq='W-FRI.
      • W-FRI is Weekly Fridays
      • pd.date_range(df.Date.min(), df.Date.max(), freq='W-FRI')
        • Creates all the Fridays in the date range between the min and max of the dates in df
      • Use a .groupby on year and month, and select .last(), to get the last Friday of every month for every year in the date range.
    • Because this method finds all the Fridays for every month in the range and then chooses .last() for each month, there's not an issue with trying to figure out which week of the month has the last Friday.
    • With this, use pandas: Boolean Indexing to find values in the Date column of the dataframe that are in last_fridays_in_daterange.
      • Use the .isin method to determine containment.
    • pandas: DateOffset objects
    import pandas as pd
    
    # test data: given a dataframe with a datetime column
    df = pd.DataFrame({'Date': pd.date_range(start=pd.to_datetime('2014-01-01'), end=pd.to_datetime('2020-08-31'), freq='D')})
    
    # create a dateframe with all Fridays in the daterange for min and max of df.Date
    fridays = pd.DataFrame({'datetime': pd.date_range(df.Date.min(), df.Date.max(), freq='W-FRI')})
    
    # use groubpy and last, to get the last Friday of each month into a list
    last_fridays_in_daterange = fridays.groupby([fridays.datetime.dt.year, fridays.datetime.dt.month]).last()['datetime'].tolist()
    
    # find the data for the last Friday of the month
    df[df.Date.isin(last_fridays_in_daterange)]
    
    0 讨论(0)
提交回复
热议问题