Python, Can't Transform Date Array to days

I have this csv file "rfm_data.csv": CustomerID PurchaseDate TransactionAmount ProductInformation 8814 11-04-23 943.31 Product C 2188 11-04-23 463.70 Product A 4608 11-04-23 80.28 Product A 2559 11-04-23 221.29 Product A I read and transform data with this code: data = pd.read_csv("rfm_data.csv") data[‘PurchaseDate’] = pd.to_datetime(data[‘PurchaseDate’], format=’%d-%m-%y’) data[‘Recency’] = (datetime.now().date() – data[‘PurchaseDate’].dt.date).dt.days When I print (data) I… Read More Python, Can't Transform Date Array to days

Python timedelta time difference from the given varied time of every day

I have the these two following pandas dataframe : df1 = {‘Date’ : [’07-10-2019′, ’07-10-2019′, ’07-10-2019′, ’08-10-2019′, ’08-10-2019′, ’08-10-2019′]} df1 = {‘Time’ :[’07-10-2019 10:47:00′, ’07-10-2019 10:52:00′, ’07-10-2019 10:59:00′, ’08-10-2019 10:47:00′, ’08-10-2019 10:52:00′, ’08-10-2019 10:59:00′, ]} and I am trying to create d[‘Time Taken’] by using: df1[‘Time Taken’]=((pd.to_datetime(df1[‘Time’])-pd.to_datetime(df1[‘Date’])).dt.total_seconds())/60 and clearly as expected, I am getting resultant… Read More Python timedelta time difference from the given varied time of every day

TypeError: unsupported operand type(s) for -: 'str' and 'datetime.timedelta'

I have a method which returns the date as "August 12, 2022" how can change that to datetime to concatenate with datetime.timedelta(days = 10) def date_return(): return date date_return() + datetime.timedelta(days=10) throws TypeError: unsupported operand type(s) for -: ‘str’ and ‘datetime.timedelta’ >Solution : You need to convert the string into a datetime object. Python doesn’t… Read More TypeError: unsupported operand type(s) for -: 'str' and 'datetime.timedelta'

python how to get the day in the median between two days

I have two timestamps: t1 = Timestamp(‘2020-01-01 00:00:00’) t2 = Timestamp(‘2020-01-07 06:00:00’)] I want to get the timestamp that is exactly in the middle between them, with one hour resolution: tm = Timestamp(‘2020-01-04 03:00:00’)] if the number of timestamps is not odd (2 values in the middle), just pick one of the two. >Solution :… Read More python how to get the day in the median between two days

check if model was recently updated fails trying to use timedelta

I have the following model that saves a datetime on save: class StockInfo(models.Model): ticker = models.CharField(max_length=100) current_price = models.FloatField() name = models.CharField(max_length=255) summary = models.TextField() sector = models.CharField(max_length=100) dividends = models.ArrayField(model_container=Dividend) last_updated_time = models.DateTimeField(null=True) objects = models.DjongoManager() # https://stackoverflow.com/questions/9953427/django-custom-save-model def save(self, *args, **kwargs): self.last_updated_time = datetime.datetime.now().astimezone() super(StockInfo, self).save(*args, **kwargs) In the view I try using… Read More check if model was recently updated fails trying to use timedelta

Pandas convert numeric year to text value till day resolution

I have dataframe like as below cust_id,purchase_date 1,10/01/1998 1,10/12/1999 2,13/05/2016 3,14/02/2018 3,15/03/2019 I would like to do the below a) display the output in text format as 5 years and 9 months instead of 5.93244 etc. I tried the below from datetime import timedelta df[‘purchase_date’] = pd.to_datetime(df[‘purchase_date’]) gb = df_new.groupby([‘unique_key’]) df_cust_age = gb[‘purchase_date’].agg(min_date=np.min, max_date=np.max).reset_index() df_cust_age[‘diff_in_days’]… Read More Pandas convert numeric year to text value till day resolution