Pandas resampling method - python

I am trying to resample a time series to get annual maximum values for different time steps(eg., 3h, 6h, etc. The original series is at an hourly resolution. I first converted the date format to pandas date format, used that column as an index, and resampled it. The final output should be the years and the corresponding maximum values at the desired timestep. However, i am getting a list of NaN. I am not sure, how can I incorporate a range in my code. Here is my code so far for a 3H timestep
import pandas as pd
df = pd.read_csv('data.txt', delimiter = ";")
df = pd.DataFrame(df[['yyyymmddhh', 'rainfall']])
datin["yyyymmddhh"] = pd.to_datetime(datin["yyyymmddhh"], format="%Y%M%d%H")
datin.set_index("yyyymmddhh").resample("3H").sum().resample("Y").max()
stn_n;yyyymmddhh;rainfall
xyz;1980123123;-
xyz;1981010100;0.0
xyz;1981010101;0.0
xyz;1981010102;0.0
xyz;1981010103;0.0
xyz;1981010104;0.0
xyz;1981010105;0.0
xyz;1981010106;0.0
xyz;1981010107;0.0
xyz;1981010108;0.0
xyz;1981010109;0.4
xyz;1981010110;0.6
xyz;1981010111;0.1
xyz;1981010112;0.1
xyz;1981010113;0.0
xyz;1981010114;0.1
xyz;1981010115;0.6

Related

Hourly Average of csv data of 15 minutes interval

My data in csv files is 15 minutes average and I want to hourly Average. When I used below code, it is showing error. 'how' unrecognised argument.
import pandas as pd
df = pd.read_csv("sirifort_with_data.csv",parse_dates=['Time_Stamp'])
data.resample('H', how='mean')
Indeed, pandas.resample does not have a keyword argument named how. You only use that function to group your time-series data. After applying the function, you could apply another to apply operations on each sample/group. Since you want to calculate the average of each group, you can use .mean():
data.resample('H').mean()
import pandas as pd
df = pd.read_csv("sirifort_with_data.csv")
df['Time_Stamp'] = pd.to_datetime(df['Time_Stamp'])
df = df.set_index('Time_Stamp').resample('H').mean()
After converting the Time_Stamp to pd.to_datetime, it worked fine.
Thanks for the help.

How to find percent increase/decrease with datetime values in Python?

I have attached a photo of how the data is formatted when I print the df in Jupyter, please check that for reference.
Set the DATE column as the index, checked the data type of the index, and converted the index to be a datetime index.
import pandas as pd
df = pd.read_csv ('UMTMVS.csv',index_col='DATE',parse_dates=True)
df.index = pd.to_datetime(df.index)
I need to print out percent increase in value from Month/Year to Month/Year and percent decrease in value from Month/Year to Month/Year.
dataframe format picture
The first correction pertains to how to read your DataFrame.
Passing parse_dates you should define a list of columns to be parsed
as dates. So this instruction should be changed to:
df = pd.read_csv('UMTMVS.csv', index_col='DATE', parse_dates=['DATE'])
and then the second instruction in not needed.
To find the percent change in UMTMVS column, use: df.UMTMVS.pct_change().
For your data the result is:
DATE
1992-01-01 NaN
1992-02-01 0.110968
1992-03-01 0.073036
1992-04-01 -0.040080
1992-05-01 0.014875
1992-06-01 -0.330455
1992-07-01 0.368293
1992-08-01 0.078386
1992-09-01 0.082884
1992-10-01 -0.030528
1992-11-01 -0.027791
Name: UMTMVS, dtype: float64
Maybe you should multiply it by 100, to get true percents.

Covert float into time in a pandas series

please see the data here: screenshot from Google Colab
I am trying to assign the time 19:00 (7pm) for all records of the column "Beginn_Zeit". For now I put the float 19.00. Now I need to convert it to a time format so that I can subsequently merge it with a date of the column "Beginn_Datum". Once I have this merged column, I need to paste its value to a all records with NaT of a different column "Delta2".
dfd['Beginn'] = pd.to_datetime(df['Beginn'], dayfirst=True)
dfd['Ende'] = pd.to_datetime(df['Ende'], dayfirst=True)
dfd['Delta2'] = dfd['Ende']-dfd['Beginn']
dfd.Ende.fillna(dfd.Beginn,inplace=True)
dfd['Beginn_Datum'] = dfd['Beginn'].dt.date
dfd["Beginn_Zeit"] = 19.00
Edited to better match your updated example.
from datetime import time, datetime
dfd['Beginn_Zeit'] = time(19,0)
# create new column combining date and time
new_col = dfd.apply(lambda row: datetime.combine(row['Beginn_Datum'], row['Beginn_Zeit']), axis=1)
# replace null values in Delta2 with new combined dates
dfd.loc[dfd['Delta2'].isnull(), 'Delta2'] = new_col

Create empy pandas DataFrame with DateTimeIndex for random time delta values

Im trying to create an empty DataFrame for which I will then constantly be appending rows to using the time stamp when the data arrives as index.
This is to code I have so far:
import pandas as pd
import datetime
df = pd.DataFrame(columns=['a','b'],index=pd.DatetimeIndex(freq='s'))
df.loc[event.get_datetime()] = event.get_data()
The problem Im having is with freq in the DateTimeIndex, the data is not arriving at any predefined intervalls, it is ju when some event tiggers. And also in the code above I need to specify a start and enddate for the index I dont want that I just want to be able to append rows whenever they arrive.
Set up empty with pd.to_datetime
df = pd.DataFrame(columns=['a','b'], index=pd.to_datetime([]))
Then do this
df.loc[pd.Timestamp('now')] = pd.Series([1, 2], ['a', 'b'])
df
a b
2018-06-10 20:52:52.025426 1 2
The first argument of DateTimeIndex is data. Try setting data to an empty list. If you want to define the start time, end time, or frequency, take a look at the other arguments for DateTimeIndex.
df = pd.DataFrame(columns=['a','b'], index=pd.DatetimeIndex([], name='startime'))
If you're trying to index on time delta values, also consider
df = pd.DataFrame(columns=['a','b'], index=pd.TimedeltaIndex([]))

Python linear interpolation of values in dataframe

I have a python dataframe with hourly values for Jan 2015 except some hours are missing the index and values both. Ideally the dataframe with columns named "dates" and "values" should have 744 rows in it. However, it has randomly missing 10 hours and hence has only 734 rows. I want to interpolate for missing hours in the month to create the desired dataframe with 744 "dates" and 744 "values".
Edit:
I am new to python so I am struggling with implementing this idea:
Create a dataframe with first column as all hours in Jan 2015
Create the second column of same size as first of NANs
Fill the second column with available values hence the missing hours have NANs in them
Use the panda interpolate funtion
Edit2:
I was looking for hint for code snippets. Based on suggestion below I was able to create the following code but it fails to fill in the values which are zeros at the start of the month i.e. for hours 1 through 5 on Jan 1.
import panda as pd
st_dt = '2015-01-01'
en_dt = '2015-01-31'
DateTimeHour = pd.date_range( pd.Timestamp( st_dt ).date(), pd.Timestamp(
en_dt ).date(), freq='H')
Pwr.index = pd.DatetimeIndex(Pwr.index) #Pwr is the original dataframe
Pwr = Pwr.reindex( DateTimeHour, fill_value = 0 )
Pwr2 = pd.Series( Pwr.values )
Pwr2.interpolate( imit_direction='both' )
Use df.asfreq to expand the DataFrame so as to have an hourly frequency. NaN is inserted for missing values:
df = df.asfreq('H')
then use df.interpolate to replace the NaNs with (linearly) interpolated values based on the DatetimeIndex and the nearest non-NaN values:
df = df.interpolate(method='time')
For example,
import numpy as np
import pandas as pd
N, M = 744, 734
index = pd.date_range('2015-01-01', periods=N, freq='H')
idx = np.random.choice(np.arange(N), M, replace=False)
idx.sort()
index = index[idx]
# This creates a toy DataFrame with 734 non-null rows:
df = pd.DataFrame({'values': np.random.randint(10, size=(M,))}, index=index)
# This expands the DataFrame to 744 rows (10 null rows):
df = df.asfreq('H')
# This makes `df` have 744 non-null rows:
df = df.interpolate(method='time')
What you want requires a combination of this technique:
Add missing dates to pandas dataframe
And the pandas function pandas.Series.interpolate. From what you've said, the option 'linear' is what you want.
EDIT:
Interpolate will not work in the case were you have datapoints missing at the very start of the time series. One idea is to use pandas.Series.fillna with 'backfill' after the interpolation. Also, do not set fill_value to 0 whe you call reindex
A general interpolation is the following:
If the key exits:
Return the value
else:
Find the first key before and after the required key, find the distance (which you can define using a desired metric) to both keys and take a weighted average of the values, weighed by the distances of the keys (close is heigher weight).

Categories