Skip to content
Advertisement

Is there any function to get multiple timeseries with .get and create a dataframe in Pandas?

I get multiple time series data in series format with datetimeindex, which I want to resample and convert to a dataframe with multiple columns with each column representing each time series. I am using separate functions to create the dataframe, for example, .get(), .resample(), pd.concat(). Since it is not following the DRY principle (Don’t Repeat Yourself) and I can be categorized as a novice programmer, it would be really appreciated if an efficient method is suggested. I am giving some snippets below:

timeseries1 = client.get('tagid', start = '2022-01-01 12:00:00', end = '2022-01-01 18:00:00')
timeseries2 = client.get('tagid', start = '2022-01-01 12:00:00', end = '2022-01-01 18:00:00')

timeseries1_resample = timeseries1.resample('1H', label = 'right').mean()
timeseries2_resample = timeseries2.resample('1H', label = 'right').mean()

df = pd.concat([timeseries1_resample, timeseries2_resample], join = 'outer', axis = 1, sort = False)

Advertisement

Answer

Although, you haven’t provided the df yet but this piece of code would definitely help:

df = pd.DataFrame({'tagid': [12, 34, 56, 78],
              'time': ['2022-01-01 12:10:00', '2022-01-01 17:00:00', '2022-01-02 18:10:00',
                          '2021-01-01 12:00:00']}) 

df['time'] = df['time'].apply(lambda x: pd.to_datetime(x))
start = pd.to_datetime('2022-01-01 12:00:00')
end = pd.to_datetime('2022-01-01 18:00:00')

df.loc[df['time'].apply(lambda x: x > start and x < end), ['tagid']]
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement