Changing row value based on previous row value

Advertisements Exist a data frame with events start_ event end_event 1 2023-09-23 17:20 2023-09-23 17:40 2 2023-09-24 10:00 2023-09-24 10:20 3 2023-09-24 10:20 2023-09-24 10:40 4 2023-09-24 10:40 2023-09-24 11:00 5 2023-09-25 11:00 2023-09-25 11:20 6 2023-09-25 11:20 2023-09-25 11:40 Difference between start_ event and end_event always 20 minutes. I need to group the data… Read More Changing row value based on previous row value

Filter pandas dataframe for first N unique values per group

Advertisements I need to filter a large dataset of variables with entries for multiple dates. In this instance I want to keep only data entered on the very first date. For example in the dataset below: dfex = pd.DataFrame({‘names’:[‘jim’,’jim’,’jim’,’jim’,’jim’,’jim’,’jim’,’jim’,’jim’, ‘bob’,’bob’,’bob’,’bob’,’bob’,’bob’, ‘sara’,’sara’,’sara’,’sara’,’sara’,’sara’,’sara’,’sara’,’sara’,’sara’], ‘dates’:[’01-01-19′,’01-01-19′,’01-01-19′,’01-05-19′,’01-06-19′,’01-07-19′,’01-08-19′,’01-09-19′,’01-10-19′, ’01-05-19′,’01-05-19′,’01-07-19′,’01-08-19′,’01-09-19′,’01-10-19′, ’01-02-19′,’01-02-19′,’01-02-19′,’01-02-19′,’01-05-19′,’01-06-19′,’01-07-19′,’01-08-19′,’01-09-19′,’01-10-19′]}) dfex[‘dates’] = pd.to_datetime(dfex[‘dates’]) dfex jim would keep the first 3 rows,… Read More Filter pandas dataframe for first N unique values per group

Exclude datetime from GroupBy sum

Advertisements I’m looking a way to use groupby on a dataframe that contains datetime64 format on some columns. But im having this error: dfsum = df.groupby([‘City’]).sum() TypeError: datetime64 type does not support sum operations I tried using: desired = df.select_dtypes(include=[int, float, object]) dfsum_city = desired.groupby([‘City’]).sum() dfsum_city.reset_index(inplace=True) But i dont think it is the right way… Read More Exclude datetime from GroupBy sum

Failing to format my date to work correctly in php

Advertisements Working on a PHP form that sends date I create a time/date from a input form. (jquery datepicker) $dateinput = $_POST[‘datepicker’]; $t1 = date_create($dateinput); //times are posted seperate and stipped from a input box (either 1055 or 10:55) $t1_hour = substr($t1_raw,0,2); $t1_min = substr($t1_raw,-2); $t1->add(new DateInterval(‘PT’ . $t1_hour . ‘H’)); $t1->add(new DateInterval(‘PT’ . $t1_min… Read More Failing to format my date to work correctly in php

Changing datetime format in sql

Advertisements I am trying to change the format of a datetime column from ‘YYYY-MM-DD HH-MM-SS’ to ‘YYYY-DD-MM HH-MM-SS’ input: 2023-04-12 07:15:10 script that is used in a select statement: TO_CHAR(TO_DATE(a.time_stamp, ‘YYYY-MM-DD HH24:MI:SS’), ‘YYYY-DD-MM HH24:MI:SS’) as CHANGE_TIME output: 0012-23-04 00:00:00 desired output: 2023-12-04 07:15:10 >Solution : That’s because you applied to_date function to value that already… Read More Changing datetime format in sql

How to create a string in PHP that is equivalent to yyyyMMddHHmmssffff

Advertisements I am trying to integrate with some VB code that generates a string using DateTime.Now.ToString("yyyyMMddHHmmssffff") The closest I can get to this in PHP has been date(‘YmdHis’) But this lacks the milliseconds, I have tried playing with microtime e.g. date(‘YmdHis’,time()).substr(str_replace(".","",microtime(true)),10,17) But sometimes this results in a shorter string >Solution : date function always returns… Read More How to create a string in PHP that is equivalent to yyyyMMddHHmmssffff

datetime struggle with a pandas dataframe

Advertisements I am running the following code: filled_df = (ts.set_index(date_col) .groupby(grouping) .apply(lambda d: d.reindex( pd.date_range( mf_heuristic(d.index, champion_end), pd.to_datetime( dt.datetime.now() ), freq=’MS’) ) ) .drop(grouping, axis=1) .reset_index(grouping) .fillna(0) with the predefinitions: date_col = ‘timepoint’ grouping = [‘Level’, ‘Kontogruppe’] champion_end = fc_date – pd.DateOffset(months=13) print(champion_end) 2021-03-01 00:00:00 print(ts) Kontogruppe Level timepoint data_value 0 A ZBFO 2020-03-01 1… Read More datetime struggle with a pandas dataframe

On parsing datetime, ValueError: time data does not match format

Advertisements I have date time in the format YYYY/DOY/HHMM DOY = Day of year I tried to parse as follows. from datetime import datetime s = "1929/189/1820" res = datetime.strptime(s, "%y/%j/%H%M") print (res) ValueError: time data ‘2029/199/1820’ does not match format ‘%y/%j/%H%M’. >Solution : The Y for year should be capitol This should fix it.… Read More On parsing datetime, ValueError: time data does not match format