> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Add Gradient Color to Date Column in Pandas
- URL: https://datascientyst.com/add-gradient-color-date-column-pandas/
- Published: 2022-02-04T15:46:37.000Z
- Updated: 2022-02-04T15:55:44.000Z
- Author: John D K
- Tags: Display

## 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:

```python
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:

```python
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:

```python
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

```python
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:

```python
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.