[Tutor] Error when trying to resample daily data to get a monthly average?
Tyler Herrington
therring at uwaterloo.ca
Tue Sep 22 16:37:03 EDT 2020
Hello,
I am attempting to read in daily soil temperature data (and later on,
hourly data) from a csv file, and calculate monthly averaged soil
temperature from the data.
The data has a structure similar to: soil temperature data output
<https://i.stack.imgur.com/GumOt.png>
Basically, the goal is to loop through all the .csv files in the directory,
read in the daily (or hourly) soil temperature data from each csv file, and
then calculate a monthly mean from the data. However, it appears that the
timestamp may not be in the correct format for resample() to correctly read
and calculate the mean, as I get the following error: *TypeError: Only
valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an
instance of 'RangeIndex'*
def load_pandas(file_name):
print("Loading file:", file_name)
dframe = pd.read_csv(file_name)
dframe.replace(-999, np.nan, inplace =True)
levels = dframe.columns.values.tolist()
print("Levels:", levels)
print(levels[0])
print("Column types:", dframe.dtypes)
print(dframe)
col1 = dframe[levels[0]]
# Sample date: 2011-06-21 08:00:00
date_fmt = "%Y-%m-%d %H:%M:%S"
datetime_column = str_to_datetime(col1, date_fmt)
# The pandas builtin seems to have issues
#datetime_column = pd.to_datetime(dframe[levels[0]], date_fmt)
print("Length of datetime column:", len(datetime_column))
dframe_mon = dframe.resample('M').mean()
print(dframe_mon)
def main():
from pathlib import Path
directory = "/praid/users/herringtont/soil_temp/In-Situ/GTN-P/"
directory_as_str = str(directory)
pathlist = Path(directory_as_str).glob('*.csv')
for path in pathlist:
# because path is object not string
path_in_str = str(path)
print(path_in_str)
boreholes = path_in_str
load_pandas(boreholes)
main()
Any suggestions on how to fix this issue?
Tyler Herrington, MSc
PhD Student (Climate Science)
Geography and Environmental Management
University of Waterloo
More information about the Tutor
mailing list