Python-Numpy-Pandas Compatible Functions¶
calcpy
provides some utility functions to provide a consistent interface to scaler, np.ndarray
, pd.Series
, and pd.DataFrame
.
Properties¶
- calcpy.shape(arg)[source]¶
Get the shape of an argument.
- Parameters:
arg
- Return type:
tuple
Examples
>>> shape(1) () >>> shape([1, 2, 3]) (3,) >>> shape(np.array([1, 2, 3])) (3,) >>> shape(pd.Series([1, 2, 3])) (3,) >>> shape(pd.DataFrame({"A": 1, "B": 2}, index=[0])) (1, 2)
- calcpy.ndim(arg)[source]¶
Get the number of dimensions of an argument.
- Parameters:
arg
- Return type:
int
Examples
>>> ndim(1) 0 >>> ndim([1, 2, 3]) 1 >>> ndim(np.array([1, 2, 3])) 1 >>> ndim(pd.Series([1, 2, 3])) 1 >>> ndim(pd.DataFrame({"A": 1, "B": 2}, index=[0])) 2
Create and Compute¶
- calcpy.broadcast_first(fun)[source]¶
Decorator for supporting
np.ndarray
,pd.Series
, andpd.DataFrame
.- Parameters:
fun (callable) – Callable that applies to a single element in its first argument.
- Returns:
- Callable that applies to a single element or a
list
,tuple
,np.ndarray
, pd.Series
, orpd.DataFrame
.
- Callable that applies to a single element or a
- Return type:
callable
Examples
>>> @broadcast_first ... def add(x, y): ... return x + y >>> add(1, 2) 3 >>> add([1, 2, 3], 2) [3, 4, 5] >>> add(np.array([1, 2, 3]), 2) array([3, 4, 5]) >>> add(pd.Series([1, 2, 3]), 2) 0 3 1 4 2 5 dtype: int64 >>> add(pd.DataFrame({"A": 1, "B": 2}, index=[0]), 2) A B 0 3 4
- calcpy.full_like(template, fill_value, **kwargs)[source]¶
Create a np.array or pd.Series or pd.DataFrame with the same shape as template.
- Parameters:
template (list | tuple | np.ndarray | pd.Series | pd.DataFrame)
fill_value – Value to populate.
**kwargs – Keyword arguments for
np.full_alike()
,pd.Series()
, orpd.DataFrame()
.
- Return type:
list | tuple | np.ndarray | pd.Series | pd.DataFrame
- Raises:
TypeError –
Examples
Create list and tuple.
>>> full_like([1, 2, 3], 0) [0, 0, 0] >>> full_like((1, 2, 3), 0) (0, 0, 0)
Create
np.array
.>>> full_like(np.array([1, 2, 3]), 0) array([0, 0, 0])
Create
pd.Series
andpd.DataFrame
.>>> full_like(pd.Series([1, 2, 3]), 0) 0 0 1 0 2 0 dtype: int64 >>> full_like(pd.DataFrame({"A": 1, "B": 2}, index=[0]), 0) A B 0 0 0
- calcpy.mapi(inputs, f)[source]¶
Apply a function on every input element and its index.
- Parameters:
inputs (list | tuple | np.ndarray | pd.Series | pd.DataFrame) – Input data to transform
f (callable) – Transformation function with some positional arguments: - For list and tuple: f(value, index) -> new_value - For ndarray: f(value, index_0, index_1, …, index_(ndim-1)) - For DataFrame: f(value, index, column) -> new_value - For Series: f(value, index, name) -> new_value
- Returns:
Transformed data with same shape/index/columns
- Return type:
list | tuple | np.ndarray | pd.Series | pd.DataFrame
Examples
Transform a list.
>>> def printall(*args): ... return ":".join(str(arg) for arg in args) >>> mapi([1, 2, 3], printall) ['1:0', '2:1', '3:2']
Transform a ndarray.
>>> from calcpy import add >>> a = np.ones(shape=(2, 3, 4)) >>> mapi(a, add) array([[[1., 2., 3., 4.], [2., 3., 4., 5.], [3., 4., 5., 6.]], [[2., 3., 4., 5.], [3., 4., 5., 6.], [4., 5., 6., 7.]]])
Transform a Series.
>>> s = pd.Series("value", index=range(3)) >>> mapi(s, printall) 0 value:0:None 1 value:1:None 2 value:2:None dtype: object
Transform a Series with datetime index and Series name.
>>> tindex = pd.date_range("2000-01-01", "2000-01-03") >>> s = pd.Series("value", index=tindex, name="name") >>> mapi(s, printall) 2000-01-01 value:2000-01-01 00:00:00:name 2000-01-02 value:2000-01-02 00:00:00:name 2000-01-03 value:2000-01-03 00:00:00:name Freq: D, Name: name, dtype: object
Transform a Series with multi-level index.
>>> mindex = pd.DataFrame({"app": "X", "date": tindex}).set_index(["app", "date"]).index >>> s = pd.Series("value", index=mindex) >>> mapi(s, printall) app date X 2000-01-01 value:('X', Timestamp('2000-01-01 00:00:00')):... 2000-01-02 value:('X', Timestamp('2000-01-02 00:00:00')):... 2000-01-03 value:('X', Timestamp('2000-01-03 00:00:00')):... dtype: object
Transform a Series to another datatype
>>> def sumlen(*args): ... return sum(len(arg) for arg in args) >>> s = pd.Series("value", index=["a", "b"], name="name") >>> mapi(s, sumlen) a 10 b 10 Name: name, dtype: int...
Transform a DataFrame.
>>> df = pd.DataFrame('hello', index=range(4), columns=range(3)) >>> mapi(df, printall) 0 1 2 0 hello:0:0 hello:0:1 hello:0:2 1 hello:1:0 hello:1:1 hello:1:2 2 hello:2:0 hello:2:1 hello:2:2 3 hello:3:0 hello:3:1 hello:3:2
Transform a DataFrame with multi-level index.
>>> df = pd.DataFrame("value", index=mindex, columns=["a"]) >>> print(mapi(df, printall)) a app date X 2000-01-01 value:('X', Timestamp('2000-01-01 00:00:00')):a 2000-01-02 value:('X', Timestamp('2000-01-02 00:00:00')):a 2000-01-03 value:('X', Timestamp('2000-01-03 00:00:00')):a
Handle empty input.
>>> s = pd.Series(dtype=object, name='empty') >>> mapi(s, printall) Series([], Name: empty, dtype: object)
Create a DataFrame whose elements are all the same as index.
>>> from calcpy import arggetter >>> index = pd.date_range("2000-01-01", "2000-01-03") >>> df = pd.DataFrame(index=index, columns=["A", "B"]) >>> mapi(df, arggetter(1)) A B 2000-01-01 2000-01-01 2000-01-01 2000-01-02 2000-01-02 2000-01-02 2000-01-03 2000-01-03 2000-01-03
Compare¶
- calcpy.overall_equal(loper, roper)[source]¶
Check whether two operands are exactly equal as a whole.
It behaves like
np.array_equal
fornp.ndarray
, andloper.equals(roper)
forpd.Series
andpd.DataFrame
.- Parameters:
loper (number | list | tuple | np.ndarray | pd.Series | pd.DataFrame)
roper (number | list | tuple | np.ndarray | pd.Series | pd.DataFrame)
- Return type:
bool
Examples
Compare lists.
>>> overall_equal([1, 2, 3], [1, 2, 3]) True
Compare
pd.DataFrame
.>>> import pandas as pd >>> df = pd.DataFrame({"A": 1, "B": 2}, index=[0]) >>> overall_equal(df, df+0) True >>> overall_equal(df, df+1) False >>> overall_equal(df, df.iloc[:, 0]) False >>> overall_equal(df["A"], df["B"]-1) True