Slicer
Slicer is a lightweight Python package designed to provide a unified and extended slicing interface for diverse tensor-like objects. It wraps standard Python data structures (lists, dicts) and scientific computing libraries (NumPy arrays, Pandas DataFrames, SciPy sparse matrices, PyTorch tensors) to enable advanced slicing functionalities like non-integer indexing and nested slicing in a single operation. The current version is 0.0.8, with its last update in 2021, suggesting a maintenance-oriented release cadence.
Warnings
- breaking Slicer v0.0.8 fixed compatibility issues due to the renaming of `scipy.sparse` classes (e.g., `csr_matrix` to `csr_array`). Projects using older Slicer versions with newer SciPy releases may encounter `AttributeError` or unexpected behavior with sparse matrix slicing.
- gotcha The library explicitly states it has '0 dependencies'. However, its core utility is to provide enhanced slicing for objects from other libraries (NumPy, Pandas, SciPy, PyTorch). To use Slicer effectively with these data types, you must install the respective underlying libraries separately.
- deprecated Behavior around alias re-assignment was corrected in Slicer v0.0.7. Prior versions may have exhibited inconsistent or unexpected results when re-assigning values to aliases tracked by a Slicer object.
- gotcha The project's last release was in 2021, and its development activity appears to be infrequent. While bug fixes have occurred, users should be aware that new features or rapid responses to external library changes might not be a priority.
Install
-
pip install slicer
Imports
- Slicer
from slicer import Slicer as S
- Alias
from slicer import Alias as A
Quickstart
from slicer import Slicer as S
import pandas as pd
import numpy as np
# Basic anonymous slicing for lists
li = [[1, 2, 3], [4, 5, 6]]
result_list = S(li)[:, 0:2].o
# result_list will be [[1, 2], [4, 5]]
# Basic anonymous slicing for dictionaries
di = {'x': [1, 2, 3], 'y': [4, 5, 6]}
result_dict = S(di)[:, 0:2].o
# result_dict will be {'x': [1, 2], 'y': [4, 5]}
# Basic named slicing for multiple objects
df = pd.DataFrame({'A': [1, 3], 'B': [2, 4]})
ar = np.array([[5, 6], [7, 8]])
sliced_objects = S(first=df, second=ar)[0, :]
# sliced_objects.first will be a pandas Series
# sliced_objects.second will be a numpy array