Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
s = pd.Series([-2.10, 3, -4.44, 5])
s.abs()
Out[2]:
0    2.10
1    3.00
2    4.44
3    5.00
dtype: float64

Absolute numeric values in a Series with complex numbers:

In [3]:
s = pd.Series([2.2 + 1j])
s.abs()
Out[3]:
0    2.416609
dtype: float64

Absolute numeric values in a Series with a Timedelta element:

In [4]:
s = pd.Series([pd.Timedelta('1 days')])
s.abs()
Out[4]:
0   1 days
dtype: timedelta64[ns]

Select rows with data closest to certain value using argsort:

In [5]:
df = pd.DataFrame({
     'p': [2, 3, 4, 5],
     'q': [20, 30, 40, 50],
     'r': [200, 50, -20, -40]
})
df
Out[5]:
p q r
0 2 20 200
1 3 30 50
2 4 40 -20
3 5 50 -40
In [6]:
df.loc[(df.r - 42).abs().argsort()]
Out[6]:
p q r
1 3 30 50
2 4 40 -20
3 5 50 -40
0 2 20 200