0

I have tried to resample a dataframe from minutes to daily. For this I used the following lines:

df_1d = df.resample('D')['Value'].sum().asfreq(freq='D').reset_index()
df_1d.set_index(pd.DatetimeIndex(df_1d['DateTime']), inplace=True)
df_1d = df_1d.drop('DateTime', axis=1)
df_1d.drop(df_1d.loc[df_1d['Value']=='0.00'].index, inplace=True)
df_1d.dropna(subset='Value', inplace=True)

My DataFrame df has two columns one called 'DateTime', containing timestamps in format %Y-%m-%d %H:%M:%S. The other contains values like 0.01, 0.05 and 0.25. The Code written so far should resample the Values, calculate the sum for each day and reset the index afterwards.
Then I want to set the Index again as a DatetimeIndex and drop off the now unsused column DateTime as there is now an index with this name.
Now I want to see only those rows, where Value is not 0. And at the end drop off all NaN-Values found in column Value.

This doesn't work as it should. At least the resampled dataframe looks like this:

            Value
DateTime
2021-09-06  0.010.050.25

It seems like my code just merge the rows for each interval instead of calculating a sum.

I also tried to use a simplier code.

df_1d = df.resample('D').sum(min_count=1).asfreq(freq='D').dropna()

But with this I got the same result.

Does anyone knows where I made a mistake?
Thanks in advance.

EDIT:
My desired output should look like this:

            Value
DateTime
2021-09-06  0.31
Grinorye
  • 23
  • 4

1 Answers1

0

Given the inputs of 0.01, 0.05, and 0.25 from your explanation, along with your sample output of 0.010.050.25, it is clear that the values in your dataframe are strings of floats instead of actual floats (so the sum operation will concatenate them). You can convert this column with pd.to_numeric with something like the following example:

import pandas as pd
df = pd.DataFrame()
df['A'] = pd.Series(['0.01', '0.05', '0.25'])
print(df.sum())
# A    0.010.050.25
# dtype: object
df['A'] = pd.to_numeric(df['A'], errors='coerce')
print(df.sum())
# A    0.31
# dtype: float64
rnorris
  • 1,571
  • 2
  • 17
  • 23