How to Add Gradient Color to Date Column in Pandas
1. Overview
In this short tutorial, we are going to add gradient color visualization for dates in Pandas DataFrame.
This is helpful when you need to distinguish dates based on their values.
2. Setup
We will use simple DataFrame:
import numpy as np
import pandas as pd
from pandas.util.testing import makeTimeSeries
s = makeTimeSeries(5)
cols = ['col_1', 'col_2']
df = pd.DataFrame(abs(np.random.randn(5, 2)), columns=cols)
df['date'] = s.index
data:
col_1 | col_2 | date | |
---|---|---|---|
0 | 0.182079 | 0.203433 | 2000-01-03 |
1 | 0.037811 | 0.096004 | 2000-01-04 |
2 | 0.479913 | 0.802299 | 2000-01-05 |
3 | 1.881718 | 1.605743 | 2000-01-06 |
4 | 0.319824 | 0.912157 | 2000-01-07 |
3. Steps to Add gradient to Date Column in Pandas
3.1. Add new column days since
We will start by adding a new column which will have the difference between some date or today and the dates in the column:
from datetime import datetime
df['days since'] = (df['date'] - datetime.today()).dt.days
DataFrame will looks like:
col_1 | col_2 | date | days since | |
---|---|---|---|---|
0 | 0.754816 | 0.107682 | 2000-01-03 00:00:00 | -8069 |
1 | 0.656553 | 0.042157 | 2000-01-04 00:00:00 | -8068 |
2 | 0.953792 | 1.593115 | 2000-01-05 00:00:00 | -8067 |
3 | 0.671552 | 1.810286 | 2000-01-06 00:00:00 | -8066 |
4 | 0.159057 | 0.548218 | 2000-01-07 00:00:00 | -8065 |
3.2. Add gradient color to days column as a Styler
Then we are going to use method background_gradient
to add gradient color to the whole DataFrame:
style1 = df.style.background_gradient(cmap='Greens')
3.3. Create new DataFrame and reuse the style
Next we are going do the following steps:
- copy the first DataFrame as
df2
- create new Styler
- hide column 'date'
- change column 'days since' to 'date' column
- reuse the first Styler
style1 = df.style.background_gradient(cmap='Greens')
df2 = df
style2 = df2.style.hide(['date'], axis='columns')
df2['days since'] = df2['date']
style2.use(style1.export())
Dates after this step will be shown as: 2000-01-03 00:00:00З
3.4. Change date format in the Pandas style object
Finally we are going to change the date format in order to make ir more readable by adding .format({"days since": lambda t: t.strftime("%m/%d/%Y")})
The dates now will be displayed as: 01/03/2000
We can use any format thanks to strftime
4. Add gradient to Date Column in Pandas - Full code
Finally we can see the full code and the final result below:
style1 = df.style.background_gradient(cmap='Greens')
df2 = df
style2 = df2.style.hide(['date'], axis='columns')
df2['days since'] = df2['date']
style2.use(style1.export()).format({"days since": lambda t: t.strftime("%m/%d/%Y")})
result:
col_1 | col_2 | days since | |
---|---|---|---|
0 | 0.754816 | 0.107682 | 01/03/2000 |
1 | 0.656553 | 0.042157 | 01/04/2000 |
2 | 0.953792 | 1.593115 | 01/05/2000 |
3 | 0.671552 | 1.810286 | 01/06/2000 |
4 | 0.159057 | 0.548218 | 01/07/2000 |
5. Conclusion
We saw how to add gradient color for dates. We cover all the steps with detailed explanation.
We also learned how to copy styles between two DataFrames and how to change the format of dates.