Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

In this assignment we study cycling in Paris, using the hourly counts of one of the bike counters that the city has installed in its streets.

This is a first contact with time series in pandas; we will practice

the companion assignment time series: events deals with series that are not on a regular grid

here is the kind of pictures we will end up with

using pandas 3.0.6
Loading...
Loading...

0. the data

there are two files in the data/ folder

fileone row percolumns
bike-counter.csvhourtimestamp, count (number of bikes that went through the counter)
weather.csvdayjour, temperature (°C, daily mean), rain_mm (daily total)

the counter is on the voie Georges Pompidou, along the Seine; it counts the bikes going in one direction only
we start with the first one

1. parsing the timestamps

loading

  1. load data/bike-counter.csv into a dataframe named df

  2. look at the first rows and at the types of the columns - what is the type of the timestamp column ?

  3. the name count is unfortunate, because df.count is a method of the dataframe;
    rename that column into n_bikes

converting

we want to turn the timestamp column into actual timestamps

  1. use pd.to_datetime() for that; what do you get ? read the message carefully

  2. look at the strings in the data: apart from the date and the hour, what else is there ?
    how many different values are there for that part ? can you guess why ?

converting, again

pandas has a hard time with a column that mixes several time zone offsets
the way out is to ask for everything to be expressed in UTC, using the utc=True option

  1. redo the conversion with that option, and store the result back into the timestamp column;
    what is the dtype now ?

  2. we are interested in the local time in Paris, since this is when people go to work
    convert that column to the timezone "Europe/Paris"
    you will need to find the right method in the .dt accessor (or do a search on tz_convert)

2. the .dt accessor

now that timestamp has a proper type, the .dt accessor gives us access to all the details
add these columns to df

columncontent
hourthe hour of the day, 0 to 23
weekdaythe day of the week, as a string like "Monday"
is_weekenda boolean, True on Saturday and Sunday
monththe month, as a number 1 to 12

the hourly profile

  1. what is the average number of bikes per hour of the day ? use a groupby and plot the result with .plot()
    what are the busiest hours ? does that match your expectations ?

  2. now do the same but separately for week days and for week ends, so that both curves show on the same plot
    we expect something like the picture in the introduction

3. using the time as the index

it is very common to use the timestamps as the index of a time series; this makes slicing very convenient

  1. make timestamp the index of df

  2. make sure the index is sorted in chronological order (check that with index.is_monotonic_increasing)

  3. what are the first and the last timestamps ? so, how long is the period covered ?

missing data

this is a counter that sits in the street, so there can be some missing data
we expect one row per hour

  1. how many rows would you expect between the first and the last timestamp ? how many do we have ?

  2. how many rows contain NaN in the n_bikes column ?

  3. which days are entirely missing ?

4. slicing

now that we have a time index, we can slice with .loc[] using strings that look like dates
let us practice, using df.loc[...] to answer these questions

  1. what are the total number of bikes counted in December 2025 ?

  2. how many rows do we have between January 1st and January 7th 2026 (both included) ?
    is the end of the slice included or not ?

  3. how many bikes in total in the morning rush, i.e. between 7:00 and 10:00 (all days included) ?

  4. how many rows do we have on 2026-03-29 ? and on 2025-10-26 ? why is that ?

is the practice growing ? (optional)

our data covers the period from September 12th to October 11th twice: in 2025 and in 2026
compare the total number of bikes in each of these 2 periods

5. resampling

resample() is like a groupby, where the groups are time intervals; it is the tool of choice to change the granularity of a time series
so, to move from one point per hour to one point per day

  1. compute daily, the total number of bikes per day (a Series), and plot it

  2. what is the busiest day ? and the least busy one ?
    hint: idxmax() and idxmin()

beware of the missing data

some days have no bike at all, which is suspicious - recall that some days were missing entirely
on the plot above they show as a few sharp drops down to 0, in the late summer and in the fall

  1. which are these days ? what happened ? what does sum() return on a group with no data at all ?

  2. fix daily so that these days show up as NaN rather than 0
    hint: look at the options of sum()

  3. plot again, but only the last 2 months, and with marker="." to see the individual points;
    what does plot() do with a NaN ?

weekly and monthly

  1. compute and plot the number of bikes per week, and per month
    in the resulting series, what is the first date for a month ? and for a week ?

  2. the first and last months are not complete; in addition, the months have different lengths
    so a monthly total is not the best way to compare them; compute the average per day instead
    hint: you can use daily

  3. make a dataframe with 3 columns total, mean and max giving, for each month, the corresponding aggregates of the daily numbers
    hint: agg()

6. adding the weather

does the weather make a difference ? let us find out with the second file

  1. load data/weather.csv into a dataframe weather, and make sure the column jour becomes a proper timestamp
    careful with the format: 12/09/2025 is meant to be September 12th; what would happen if pandas chose to read it as December 9th ?
    use the format= option of pd.to_datetime to say what you mean

  2. make that column the index of weather

  3. combine daily and weather into one dataframe both, with the 3 columns n_bikes, temperature and rain_mm
    at that point you may run into an issue with the indexes - that is because one has a time zone, and the other one has not
    we recommend to remove the time zone from daily, you can use tz_localize(None) on its index

a look at the result

  1. are there less bikes on rainy days ?
    let us say a day is rainy if there is more than 1mm of rain
    compare the average number of bikes on rainy days and on other days

  2. draw a scatter plot of the number of bikes against the temperature, using df.plot.scatter() or similar

  3. what is the correlation between the 3 columns ?
    hint: corr()

appendix: where the data comes from