You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importpandasaspdimportnumpyasnpdefget_closest_slope(csv_file_path, transect_id, target_date:str,default_slope=0.0):
provided_date=pd.to_datetime(target_date)
df=pd.read_csv(csv_file_path, header=None)
# Rename the first column to 'date' for better readabilitydf.rename(columns={0: 'date'}, inplace=True)
# Convert the 'date' column to datetime formatdf['date'] =pd.to_datetime(df['date'])
# set the date column as the indexdf.set_index('date', inplace=True)
# get the first row and turn it into the columns exception the first columndf.columns=df.iloc[0].values# drop the first row because it contains the column namesdf=df[1:]
iftransect_idnotindf.columns:
raiseValueError(f"Transect ID {transect_id} not found in the CSV file")
ifnp.all(df[transect_id].isna()):
raiseValueError(f"All slope values for transect {transect_id} are NaN")
# Filter non-NA values for the given transect IDtransect_id_dates=df[transect_id].dropna()
# Find the index of the date closest to the provided dateclosest_date_idx=np.abs(transect_id_dates.index-provided_date).argmin()
# Get the value (slope) at the closest dateclosest_slope=transect_id_dates.iloc[closest_date_idx]
returnclosest_slope# # Example usagecsv_file_path="TrucVert_fieldsurvey_in_situ_slopes.csv"target_date='2011-08-02'transect_id=840.0# Example transect IDslope=get_closest_slope(csv_file_path, transect_id, target_date)
slope
The text was updated successfully, but these errors were encountered:
Example Data
TrucVert_fieldsurvey_in_situ_slopes.csv
Initial Code
The text was updated successfully, but these errors were encountered: