Questions tagged [relativedelta]

The relativedelta type, as defined by the datetutil module, is designed to be applied to an existing datetime and can replace specific components of that datetime, or represents an interval of time.

50 questions
15
votes
3 answers

dateutil.relativedelta - How to get duration in days?

I wish to get the total duration of a relativedelta in terms of days. Expected: dateutil.timedelta(1 month, 24 days) -> dateutil.timedelta(55 days) What I tried: dateutil.timedelta(1 month, 24 days).days -> 24 (WRONG) Is there a simple way to do…
user2422457
5
votes
2 answers

relativedelta in python when working in months

Suppose I am working with relative delta when it comes to months. The problem is when I do: x = relativedelta(months=12) x.months > 0 This is a problem when I try to do something like this: y = relativedelta(months=12) -…
theamateurdataanalyst
  • 2,794
  • 4
  • 38
  • 72
4
votes
5 answers

How do I get an age in years and date on pandas

Here's my data Customer_id Date-of-birth 1 1992-07-02 2 1991-07-03 Here's my code import datetime as dt df['now'] = dt.datetime.now() df['age'] = df['now'].dt.date - df['Date-of-birth'] Here's the result Customer_id …
Nabih Bawazir
  • 6,381
  • 7
  • 37
  • 70
3
votes
1 answer

Django - F('datetimefield') + relativedelta(months=1) fails

I am trying to update a self datetime field of a model using F() object in django. HeatWatchList.objects.filter( user=request.user, next_date_to__lt=datetime.combine(datetime.now().date(), time.min) ).update( next_date_from =…
Shift 'n Tab
  • 8,808
  • 12
  • 73
  • 117
3
votes
1 answer

Accessing python dateutil relativedelta values

I am very new to python and I've run across a small problem that I haven't been able to find an answer to by googling. I am running the following code: from dateutil import relativedelta as rdelta def diff_dates(date1, date2): return…
2
votes
0 answers

Incompatibility of relativedelta

Python 3.8.6 It is surprising that im not able to find the exact same errors in any google results but, it seems datetime's timedelta and relativedelta from relativedelta module cannot be compared. Here is the code to replicate: from datetime import…
figs_and_nuts
  • 4,870
  • 2
  • 31
  • 56
2
votes
3 answers

Create a list of sequent year month in Python

I try to create a list of year-month from 2020-06 (last month from today) to 2021-05 using the following code: from datetime import datetime from dateutil.relativedelta import relativedelta need_months = list(range(-1, 12)) print([(datetime.today()+…
ah bon
  • 9,293
  • 12
  • 65
  • 148
2
votes
1 answer

Selecting Data from Last X Months

I want to select data from the last 4 months. I would want to start from the beginning of the month, so if it is currently July 28, I would want data from March1-July28. Currently I use DateOffset, and I realized that it is calling March28-July28…
jenryb
  • 2,017
  • 12
  • 35
  • 72
1
vote
2 answers

TypeError: unsupported operand type(s) for +: 'DatetimeArray' and 'relativedelta'

I am trying to convert a column called Month_Next from a dataframe called df_actual from the last day of one month to the first day of the next. The column looks like this: And I'm using df_actual.Month_Next = pd.to_datetime(df_actual.Month_Next) +…
hulio_entredas
  • 675
  • 1
  • 12
1
vote
2 answers

Wrong timezone shift after adding a time delta over DST

I'm trying to shift a tz-aware timestamp for one month. Since datetime.timedelta does not have a month option, package relativedelta should be used. The problem arises when the time point is after the summer DST and the result after the shift will…
Amir P
  • 123
  • 5
1
vote
2 answers

Aggregate elements of a list containing relativedelta objects

I have a list myList that contains relativedelta objects. I want to add all of these relativedeltas objects. I tried: sum(myList) but it gives TypeError. TypeError: unsupported operand type(s) for +: 'int' and 'relativedelta' Even though…
Shaida Muhammad
  • 1,428
  • 14
  • 25
1
vote
2 answers

Question about dateutil.relativedelta - Why the ouput is always zero?

Why the output of this relativedelta attribute is also zero? The data file contains two date time strings, the purpose is to get the time difference between the two. # python3.6 time_diff.py 0 0 0 0 # cat data 06/21/2019 21:45:24 06/21/2020…
idiot one
  • 314
  • 1
  • 4
  • 11
1
vote
0 answers

Get first and last date of previous month in Django

I need to get the first and last date of the previous month from today. curr_ = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0) curr_date = curr_.date() prev_date = curr_date - relativedelta(months=1) The above lines…
Muhammad Zahid
  • 355
  • 2
  • 13
1
vote
1 answer

Strange arithmetic with relativedelta for date calculations

I am using relativedelta to calculate difference between two dates. However, it seems like the year part of the datetime object isn't being recognized. If the dates were of the same year-months, the calculations obtained would make sense. # lease…
kms
  • 1,810
  • 1
  • 41
  • 92
1
vote
3 answers

Measuring months between two dates : legislative definition of months

I'm looking to build code that represents the definition of months in a piece of Australian legislation - the Interpretations Act (1987). Please note that I am still a relative novice to Python. The legal definition The definition reads as…
1
2 3 4