facing error while parsing date column with different formats

Advertisements my dataframe includes date in dd/mm/yy & dd/mm/yyyy format but while converting them into pandas datetime it gives below error : ValueError: time data "02/11/21" doesn’t match format "%m/%d/%Y", at position 129. You might want to try: – passing format if your strings have a consistent format; – passing format=’ISO8601′ if your strings are… Read More facing error while parsing date column with different formats

Unexpected behaviour datetime.strptime parse when the format string lacks spaces

Advertisements Trying to parse and validate a date and hour that has to have "yyyymmddhh" format. I want the function to raise an exception if the string does not conform the specified format, so I test two ill formed strings that hasn’t the hour part: Test 1. Results as expected >>> datetime.strptime("20230609", "%Y%m%d%H") Traceback (most… Read More Unexpected behaviour datetime.strptime parse when the format string lacks spaces

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

Advertisements 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 +… 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

Advertisements 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… 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

Advertisements 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… Read More Python how to format a string time from number time to name time

Timeseries – group data by specific time period slices

Advertisements 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… Read More Timeseries – group data by specific time period slices

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

Advertisements 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.… Read More Python – Count duplicate user Id's occurence in a given month