!ls "{project.project_dir}"ncei-climate-karachi.csv
Climate change is impacting the way people live around the world
Higher highs, lower lows, storms, and smoke – we’re all feeling the effects of climate change. In this workflow, you will take a look at trends in temperature over time in Karachi, Pakistan.
In a bulleted list, how is climate change affecting your home?
For this challenge, you’ll be running a scientific workflow in Python. But something’s wrong – The code won’t run! Your task is to follow the instructions below to clean and debug the Python code below so that it runs.
Don’t worry if you can’t solve every bug right away. We’ll get there! If you are working on one bug for more than about 10 minutes, it’s time to ask for help.
Alright! Let’s clean up this code.
Before we get started, let’s define some parameters. You can use these if you want to change how the workflow runs from the top:
Because Python is open source, lots of different people and organizations can contribute (including you!). Many contributions are in the form of packages which do not come with a standard Python download.
Learn more about using Python packages. How do you find and use packages? What is the difference between installing and importing packages? When do you need to do each one? This article on Python packages will walk you through the basics.
In the cell below, someone was trying to import the pandas package, which helps us to work with tabular data such as comma-separated value or csv files (e.g. data with rows and columns like a spreadsheet). But something’s wrong!
pandas package under its alias pd.# symbol, just like we did for you with the earthpy package.Next, lets download some climate data from Karachi, Pakistan to practice with. The data will come in comma-separate value, or CSV format.
Learn more about tabular data and CSV files in the this article on text files in Earth Data Science.
Project Name Here with the actual project name, Karachi Climate.data-folder-name-here with a descriptive name for your data folder.Downloading from https://ndownloader.figshare.com/files/55245161
PosixPath('/home/runner/.local/share/earth-analytics/karachi-climate')
If you are on GitHub Codespaces, you should be able to see your data in your Explorer tab.

data folder mounted there.You can also take a look at your data using the bash programming language, either in your terminal or here in your Jupyter notebook (the ! indicates to use the current bash process, and the {} indicates to use a Python variable):
The pandas library you imported can download data from the internet directly into a type of Python object called a DataFrame. In the code cell below, you can see an attempt to do just this. But there are some problems…
Make any changes needed to get this code to run. HINT: The filename.csv isn’t correct - you need to replace it with the name of the file you downloaded! See if you can find where the data downloaded to.
The pd.read_csv() function isn’t formatting the data 100% correctly. Modify the code to include the following additional parameters, making sure to put a comma (,) in-between each parameter:
index_col='DATE' – this sets the DATE column as the index. Needed for subsetting and resampling later onparse_dates=True – this lets python know that you are working with time-series data, and values in the indexed column are date time objectsna_values=['NaN'] – this lets python know how to handle missing valuesWe can’t get the data back later on because it isn’t saved in a variable. In other words, we need to give the url a name so that we can request in from Python later (sadly, Python has no ‘hey what was that thingy I typed yesterday?’ function). Make sure to use an expressive variable name so you remember what it is later on!
One of the most common challenges for new programmers is making sure that your results are stored so you can use them again. In Python, this is called naming, or saving a variable. Learn more in this hands-on activity on using variables from our learning portal.
| STATION | TAVG | |
|---|---|---|
| DATE | ||
| 1942-10-01 | PKM00041780 | 81 |
| 1942-10-02 | PKM00041780 | 81 |
| 1942-10-03 | PKM00041780 | 84 |
| 1942-10-04 | PKM00041780 | 84 |
| 1942-10-05 | PKM00041780 | 84 |
| ... | ... | ... |
| 2024-09-26 | PKM00041780 | 87 |
| 2024-09-27 | PKM00041780 | 87 |
| 2024-09-28 | PKM00041780 | 86 |
| 2024-09-29 | PKM00041780 | 87 |
| 2024-09-30 | PKM00041780 | 87 |
19371 rows × 2 columns
Check out the type() function below - you can use it to check that your data is now in DataFrame type object.
DataFrameYou can use double brackets ([[ and ]]) to select only the columns that you want from your DataFrame:
some_column_name to the Temperature column name.It’s important to keep track of the units of all your data. You don’t want to be like the NASA team who crashed a probe into Mars because different teams used different units!
One way to keep track of your data’s units is to include the unit in data labels. In the case of a DataFrame, that usually means the column names.
A big part of writing expressive code is descriptive labels. Let’s rename the columns of your dataframe to include units. Complete the following steps:
dataframe with the name of your DataFrame, and dataframe_units with an expressive new name.'temperature-column-name' with the temperature column name in your data, and 'temp_unit' with a column name that includes the correct unit. For example, you could make a column called 'temperature_k' to note that your temperatures are in degrees Kelvin.In this case, we want to convert data from degrees Fahrenheit to degrees Celcius. The equation for converting Fahrenheit temperature to Celcius is:
\[T_C = (T_F - 32) * \frac{5}{9}\]
The code below attempts to convert the data to Celcius, using Python mathematical operators, like +, -, *, and /. Mathematical operators in Python work just like a calculator, and that includes using parentheses to designate the order of operations.
This code is not well documented and doesn’t follow PEP-8 guidelines, which has caused the author to miss an important error!
Complete the following steps:
dataframe with the name of your DataFrame.'old_temperature' with the column name you used; Replace 'new_temperature' with an expressive column name.| temp_f | temp_c | |
|---|---|---|
| DATE | ||
| 1942-10-01 | 81 | 27.222222 |
| 1942-10-02 | 81 | 27.222222 |
| 1942-10-03 | 84 | 28.888889 |
| 1942-10-04 | 84 | 28.888889 |
| 1942-10-05 | 84 | 28.888889 |
| ... | ... | ... |
| 2024-09-26 | 87 | 30.555556 |
| 2024-09-27 | 87 | 30.555556 |
| 2024-09-28 | 86 | 30.000000 |
| 2024-09-29 | 87 | 30.555556 |
| 2024-09-30 | 87 | 30.555556 |
19371 rows × 2 columns
Using the code below as a framework, write and apply a function that converts to Celcius. You should also rewrite this function name and parameter names to be more expressive.
Plotting in Python is easy, but not quite this easy:
Looks like we have both temperature units on the same plot, and it’s hard to see what it is because it’s missing labels!

Make sure each plot has:
When plotting in Python, you’ll always need to add some instructions on labels and how you want your plot to look.
dataframe to your DataFrame name.y= to the name of your temperature column name.title, ylabel, and xlabel parameters to add key text to your plot.figsize=(x,y) where x is figure width and y is figure heightLabels have to be a type in Python called a string. You can make a string by putting quotes around your label, just like the column names in the sample code (eg y='temperature').

There are many other things you can do to customize your plot. Take a look at the pandas plotting galleries and the documentation of plot to see if there’s other changes you want to make to your plot. Some possibilities include:
Not sure how to do any of these? Try searching the internet, or asking an AI!
You may notice that your plot looks a little “fuzzy”. This happens when Python is trying to plot a value for every date, but the resolution of the image is too low to actually do that. You can address this issue by resampling the data, or summarizing it over a time period of your choice. In this case, we will resample annually, giving us one data point per year.
DT_OFFSETwith a Datetime Offset Code. Check out the table in the pandas datetime documentation to find the one you want (we recommend the start of the year).agg_method_here with a method that will calculate the average annual value. Check out the pandas resampling documentation for a list of common built-in options.| temp_f | temp_c | |
|---|---|---|
| DATE | ||
| 1942-01-01 | 74.597826 | 23.665459 |
| 1943-01-01 | 78.654795 | 25.919330 |
| 1944-01-01 | 78.423497 | 25.790832 |
| 1945-01-01 | 77.786301 | 25.436834 |
| 1946-01-01 | 76.164474 | 24.535819 |
| ... | ... | ... |
| 2020-01-01 | 81.229508 | 27.349727 |
| 2021-01-01 | 81.617729 | 27.565405 |
| 2022-01-01 | 81.257618 | 27.365343 |
| 2023-01-01 | 81.391185 | 27.439547 |
| 2024-01-01 | 83.018939 | 28.343855 |
83 rows × 2 columns
Following the PEP-8 style guide is important because it makes your code easy for you and other collaborators to read. When you are splitting function calls across multiple lines, your code should look like this:
or maybe this:

You can use the .hvplot() method with similar arguments to create an interactive plot.
.plot in your code with .hvplotNow, you should be able to hover over data points and see their values!
You will need to save your analyses and plots to tell others about what you find.
Just like with any other type of object in Python, if you want to reuse your work, you need to give it a name.
hvplot code, and give your plot a name by assigning it to a variable. HINT: if you still want your plot to display in your notebook, make sure to call its name at the end of the cell.my_plot with the name you gave to your plot.'my_plot.html' with the name you want for your plot. If you change the file extension, .html, to .png, you will get an image instead of an interactive webpage, provided you have the necessary libraries installed.Once you run the code, you should see your saved plot in your files – go ahead and open it up.
If you are working in GitHub Codespaces, right-click on your file and download it to view it after saving.