I have a Pandas DataFrame with daily data that I’m trying to group by week number to sum some columns, and I notice that when years do not begin on Sunday, the data for the week spanning the end of one year and the beginning of the next do not cleanly sum, instead being broken into two groups. My code
Tag: datetime
Add missing timestamps for each different ID in dataframe
I have two dataframes (simple examples shown below): df1 shows every timestamp I am interested in. df2 shows data sorted by timestamp and ID. What I need to do is add every single timestamp from df1 that is not in df2 for each unique ID and add zero to the value column. This is the outcome I’m interested in My
how can i calculate the difference between two dates and times in python
my code looks like this: listaDivisa looks like this: I want to calculate how much time (better in minutes) the sensor was ON, I tried doing this: but it doesn’t work, another raw alternative I tried was but, not minding about the fact that it doesn’t even work, I don’t think it could be a smart alternative. How could I
Matplotlib dates.DateFormatter forcing the display of nanoseconds
I encounter an issue with Matplotlib.dates.DateFormatter : I want to convert timestamps in Date format which is simple usually with the straftime but when using it on matplotlib i don’t have the dynamic position on my graph so I used the md.DateFormatter(‘%H:%M:%S.%f’) to have the X values as a date format with the dynamic index. The fact is, my dates
Separate columns of a DataFrame by days of the week
Let it be the following Python Panda Dataframe (the original could include dates for several months): Hours 2022-06-06 2022-06-07 2022-06-08 2022-06-09 2022-06-10 2022-06-11 2022-06-12 2022-06-13 2022-06-14 2022-06-15 2022-06-16 2022-06-17 2022-06-18 2022-06-19 00:00 3 0 0 3 23 43 1 2 3 3 7 3 1 0 05:00 5 4 0 3 32 31 3 9 3 3 5 3 0
Adding different days in a DataFrame with a fixed date
I have a DataFrame with numbers (‘number’) and I wanted to add these numbers to a date. Unfortunately my attempts don’t work and I always get error messages no matter how I try…. This is a code example how I tried it: As an example of the result (YYYY/MM/DD) should come out a column or DataFrame with a date, which
Calculate activity interval for a pandas DataFrame with datetime rows
Given the following DataFrame of pandas in Python: Displays the switching on and off of 3 light bulbs at different times using date and time objects. I want to add a new column, called cost_days. This column will include only for rows where the variable using_time is different from NaT. Information on how many times the light bulb has been
how to divide a datetime object by an int
I can’t seem to divide a datetime.time object by an int. Looking at the documentation I can’t figure out why it isn’t working. I get the error: unsupported operand type(s) for /: ‘datetime.time’ and ‘int’. I did see a method that splits the time down into second then performs the division on the number of seconds, but wondering if there
Week number not matching with datetime index strftime week number in pandas
I have the following code which converts index to week number as well as another column is added with week number: As you can see there are instances where week 11 is being shown in the index column using df.index.strftime(‘%U’) when the week number is showing as 12 using df.index.isocalendar().week Why are there week number different in index column as
Pandas dataframe – fillna with last of next month
I’ve been staring at this way too long and I think Ive lost my mind, it really shouldn’t be as complicated as I’m making it. I have a df: Date1 Date2 2022-04-01 2022-06-17 2022-04-15 2022-04-15 2022-03-03 NaT 2022-04-22 NaT 2022-05-06 2022-06-06 I want to fill the blanks in ‘Date2’ where it keeps the values from ‘Date2’ if they are present