# Detrend a timeseries in Python

To find inter-annual variability and correlation between two time series variable that excludes the influence of external forcing (i.e. global warming), we might want to detrend the time series of variables. For detrending, we will use scipy package of python.

Let's say if we have sea level pressure variable with 38 years of data that has 360 longitude and 180 latitude. First will import scipy package in our python library.

example pip install cmd: *python -m pip install --user numpy scipy *

*For more follow this link*

Now example script for detrending:

`import numpy as np`

`from scipy import signal`

Now read the data. For example I'm using SLP

`slp.shape`

`> (38,180,360) #time, lat, lon`

Now use signal function to detrend the variable:

`slp_detrended=np.zeros(slp.shape)`

`for i in range(len(lat)):`

` for j in range(len(lon)):`

` slp_detrended[:,i,j]= signal.detrend(slp[:,i,j])`

**What is data detrending:** Detrending is **removing a **trend **from a **time series; a trend usually refers to a change in the mean over time. When you detrend data, you remove an aspect from the data that you think is causing some kind of distortion. For example, you might detrend data that shows an overall increase, in order to see subtrends. Usually, these subtrends are seen as fluctuations on a time series graph. (source)