Filter dataframe for past 4 quarters

I have a DataFrame with start date and end date as a column. Firstly I need create a new column using End Date which consists of yearquarter (2022Q4) which I did using below code: df[‘Quarter’]=pd.PeriodIndex(d[‘End Date’],freq=’Q’) Now I want to filter data for past 4 quarters using End Date, lets say curremtly its 2023Q1, so… Read More Filter dataframe for past 4 quarters

How to find the last date of the current week or quarter in python

I would like to find a simple way to find the last date of the current week or quarter. To find the last date of the current month I can use the relativdelta function from dateutil: import pandas as pd today_date = pd.Timestamp.today().date() #get today’s date from dateutil.relativedelta import relativedelta current_month_last_date = today_date + relativedelta(day=31)… Read More How to find the last date of the current week or quarter in python

Dataframe – Converting entire column from str object to datetime object – TypeError: strptime() argument 1 must be str, not Series

I want to convert values in entire column from strings to datetime objects, but I can’t accomplish it with this code which works on solo strings i.e. (if I add .iloc[] and specify the index): price_df_higher_interval[‘DateTime’] = datetime.datetime.strptime(price_df_higher_interval[‘DateTime’], ‘%Y-%m-%d %H:%M:%S’) Also I would like to ommit looping through the dataframe, but I don’t know if… Read More Dataframe – Converting entire column from str object to datetime object – TypeError: strptime() argument 1 must be str, not Series

Python how to format a string time from number time to name time

I have a string time : ctime = ‘Thu Sep 1 12:25:26 2022’ How can I format it to : 01 Sep 2022 12:25:26 I have tried: ctime .strftime("%d %m %Y, %H:%M:%S") But received: AttributeError: ‘str’ object has no attribute ‘strftime’ Any friend can help ? >Solution : from datetime import datetime from datetime import… Read More Python how to format a string time from number time to name time

Timeseries – group data by specific time period slices

I am working with a csv file that has a dataset of 13 years worth of 5m time intervals. I am trying to slice sections of this dataset into specific time periods. example time_period = (df[‘time’] >= ’01:00:00′) & (df[‘time’]<=’5:00:00′) time_period_df = df.loc[time_period] I would expect an output of only the time between 1-5 to… Read More Timeseries – group data by specific time period slices

Python – Count duplicate user Id's occurence in a given month

If I create a Dataframe from df = pd.DataFrame({"date": [‘2022-08-10′,’2022-08-18′,’2022-08-18′,’2022-08-20′,’2022-08-20′,’2022-08-24′,’2022-08-26′,’2022-08-30′,’2022-09-3′,’2022-09-8′,’2022-09-13’], "id": [‘A’,’B’,’C’,’D’,’E’,’B’,’A’,’F’,’G’,’F’,’H’]}) df[‘date’] = pd.to_datetime(df[‘date’]) (Table 1 below showing the data) I am interested in counting how many times an ID appears in a given month. For example in a given month A, B and F all occur twice whilst everything else occurs once. The… Read More Python – Count duplicate user Id's occurence in a given month

Difference in time between successive dataframe rows

Similar to this question, I would like to compute the time difference between rows of a dataframe. Unlike that question however, the difference should be by groupby id. So foe example, this dataframe: df = pd.DataFrame( {‘id’: [6,6,6,6,6,10,10,10,10,10], ‘timestamp’: [‘2016-04-01 00:04:00′,’2016-04-01 00:04:20′,’2016-04-01 00:04:30’, ‘2016-04-01 00:04:35′,’2016-04-01 00:04:54′,’2016-04-30 13:04:59’, ‘2016-04-30 13:05:00′,’2016-04-30 13:05:12′,’2016-04-30 13:05:20’, ‘2016-04-30 13:05:51’]} ) df.head()… Read More Difference in time between successive dataframe rows

discord.py How can I send a message if difference between the time of message and current time is above time I specified?

timestamp = f'{message.created_at}’ msg_time = datetime.strptime(timestamp, ‘%Y-%m-%d %H:%M:%S.%f’) now_time = datetime.now() diff = now_time – msg_time print(‘msg_time:’, msg_time) print(‘now_time:’, now_time) print(‘diff :’, diff) output : msg_time: 2022-07-22 06:02:12.934000 now_time: 2022-07-23 01:53:52.375086 diff : 19:51:39.441086 If diff is greater than the time I specify, I want it to send a message to the channel like this:… Read More discord.py How can I send a message if difference between the time of message and current time is above time I specified?

unsupported operand type(s) for &: 'DatetimeArray' and 'DatetimeArray'

Sanctioned Date:- DateTime column a = df[df[‘Sanctioned Date’] >= ‘2022-01-01’] b = df[df[‘Sanctioned Date’] <= ‘2022-06-16’] want :- a & b >Solution : Output of a, b is filtered DataFrame, so cannot chain by &. You can chain masks with (): out = df[(df[‘Sanctioned Date’] >= ‘2022-01-01’) & (df[‘Sanctioned Date’] <= ‘2022-06-16’)] Or use Series.between:… Read More unsupported operand type(s) for &: 'DatetimeArray' and 'DatetimeArray'

format date in python datatime

I wanted to format the string last_date to d/m/Y. Without formatting it runs but I need formatting from datetime import date from datetime import datetime from datetime import timedelta from calendar import monthrange data = date.today() data_atual = data.strftime(‘%d/%m/%Y’) print(data_atual) data_e_hora_atuais = datetime.now() fuso_horario = timezone("America/Sao_Paulo") data_e_hora_sao_paulo = data_e_hora_atuais.astimezone(fuso_horario) data_e_hora = data_e_hora_sao_paulo.strftime("%d/%m/%Y %H:%M") print(data_e_hora) last_date… Read More format date in python datatime