-2

I have the code below:

import datetime

def get_date(x, y):
    last_day = date.today().replace(day=1) - timedelta(days=1)
    start_day= date.today().replace(day=1) - timedelta(days=last_day_of_prev_month.day)
    start_day_of_prev_month = start_day.strftime('%Y-%m-%d')
    last_day_of_prev_month= last_day.strftime('%Y-%m-%d')

    today = date.today().strftime('%Y-%m-%d')
    firstDayOfMonth = date.today().replace(day=1).strftime('%Y-%m-%d')
    lastDay = sf.date_sub(sf.current_date(), 1)
    if today == firstDayOfMonth:
        x = start_day_of_prev_month,
        y = last_day_of_prev_month
    else:
        x = firstDayOfMonth,
        y = lastDay
    return 0

I need it to return two dates according to the "if" above 'x','y'. The output would be for example:

'2022-08-01','2022-08-18' 

But, this function don't return this output.

Can anyone help me?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Vivian
  • 95
  • 5
  • `return (date1, date2)` -> then you can reference them in whatever variable you store the output from your function like so: `date1 = my_var[0] -> date1` – Shmack Aug 19 '22 at 17:32
  • can you write for me? I started python not long ago – Vivian Aug 19 '22 at 17:37
  • From what I can tell, you're asking how to return two values from a function. so I've closed your question accordingly. If that doesn't answer your question, please [edit] to clarify. And please read [How to ask a good question](/help/how-to-ask), which has tips like starting with your own research and how to write a good title. – wjandrea Aug 19 '22 at 17:46

2 Answers2

1

This implements the following logic: If today is the first day of the month, return (first day of previous month, last day of previous month), otherwise return (first day of current month, yesterday):

def get_data():
    today = date.today()
    if today.day == 1:
        x = today.replace(month=today.month-1, day=1)
        y = today.replace(day=1) - timedelta(days=1)
    else:
        x = today.replace(day=1)
        y = today - timedelta(days=1)
    return x, y

or even simpler, as this is equivalent to (first day of yesterday's month, yesterday):

def get_data():
    today = date.today()
    y = today - timedelta(days=1)
    x = y.replace(day=1)
    return x, y
Michael Hodel
  • 2,845
  • 1
  • 5
  • 10
  • Yes, exact logic! But for the output, how I do? – Vivian Aug 19 '22 at 17:45
  • Simply call the function and print what it returned, i.e. `print(get_data())` – Michael Hodel Aug 19 '22 at 17:47
  • One doubt, I need use the output in function between pyspark for example: `test_df.filter(F.col("start").between('2017-04-13','2017-04-14'))`, but when I put `test_df.filter(F.col("start").between(get_data()))` return this error: between() missing 1 required positional argument: 'upperBound' Do you know how to resolve this error? – Vivian Aug 19 '22 at 18:13
  • 1
    `pd.Series([today]).between(*get_data())` – Michael Hodel Aug 19 '22 at 18:15
0

To return a tuple from a function, you can simply

return x, y

then you can store an output of such function as a tuple and access its items via indexes:

result_tuple = get_date(x, y)
first = result_tuple[0]
second = result_tuple[1]

or split the result into variables, if you know how many items will be in returned the tuple:

first, second = get_date(x, y)
honorius
  • 481
  • 4
  • 7