Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Why do I get negative temperature values?

$
0
0

I have a problem with my code. I want to get average temperature for the city of Kiev, but I get negative temperature values for all seasons and I don't know why. It's nothing wrong with the conversion from fahrenheit to celsius.

Here is the code:

# Selecting temperature data from Kievkiev_df = df[df["City"] == "Kiev"].copy()# Converting fahr to celsiuskiev_df = fahr_to_celsius(kiev_df)# Converting "dt" column to datetime formatkiev_df.loc[:, "dt"] = pd.to_datetime(kiev_df["dt"], format="%Y%m%d")def get_season(month):"""    Dividing months into seasons.    Parameters: month (int): Month number (1 for January, 2 for Februrary, etc.)    Returns:     str: The season corresponding to input months."""    if month in [12, 1, 2]:        return "Winter"    elif month in [3, 4, 5]:        return "Spring"    elif month in [6, 7, 8]:        return "Summer"    else:         return "Autumn"# Map get_season function to the month of each date in "dt" columnkiev_df.loc[:, "Season"] = kiev_df["dt"].dt.month.map(get_season)# Group data by year and season, calculate the mean of temp dataseasonal_avg = kiev_df.groupby([kiev_df["dt"].dt.year, "Season"]).agg({"AverageTemperature": "mean" ,"Tuncertainty": "mean"}).reset_index()seasonal_avg.columns = ["Year", "Season", "AvgTemperature", "AvgUncertainty"]print(seasonal_avg)

I try to get average temperature for all seasons, but I get negative values and that is not correct.

And here is the code for convertion

Creating function to convert from Fahrenheit to Celsius

def fahr_to_celsius(df):"""Converts Fahrenheit temperature to Celsius (excluding the "Tuncertainty" column)Parameters:- df (DataFrame): DataFrame containing temperature data.Returns:- DataFrame: A modified DataFrame with temperature columns converted from Fahrenheit to Celsius values."""# Converting these columns from fahr to celsiuscelsius_conv = ["TMAX", "TMIN", "AverageTemperature"]for col in celsius_conv:    df[col] = (df[col] - 32) / 1.8return df

Here is a sample of the data:

    dt  AverageTemperature  Tuncertainty    City    Country TMAX        TMIN114929  17440401    49.676      4.4964      Kiev    Ukraine 54.1724     45.1796114930  17440501    55.6556     3.321       Kiev    Ukraine 58.9766     52.3346114931  17440601    63.3074     3.0654      Kiev    Ukraine 66.3728     60.242114932  17440701    66.9002     2.8656      Kiev    Ukraine 69.7658     64.0346114933  17440801    -9999       2.9466      Kiev    Ukraine 68.28430418 63.60738403

Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>