pyspark.pandas.
CategoricalIndex
Index based on an underlying Categorical.
CategoricalIndex can only take on a limited, and usually fixed, number of possible values (categories). Also, it might have an order, but numerical operations (additions, divisions, …) are not possible.
The values of the categorical. If categories are given, values not in categories will be replaced with NaN.
The categories for the categorical. Items need to be unique. If the categories are not given here (and also not in dtype), they will be inferred from the data.
Whether or not this categorical is treated as an ordered categorical. If not given here or in dtype, the resulting categorical will be unordered.
If CategoricalDtype, cannot be used together with categories or ordered.
CategoricalDtype
Make a copy of input ndarray.
Name to be stored in the index.
See also
Index
The base pandas-on-Spark Index type.
Examples
>>> ps.CategoricalIndex(["a", "b", "c", "a", "b", "c"]) CategoricalIndex(['a', 'b', 'c', 'a', 'b', 'c'], categories=['a', 'b', 'c'], ordered=False, dtype='category')
CategoricalIndex can also be instantiated from a Categorical:
Categorical
>>> c = pd.Categorical(["a", "b", "c", "a", "b", "c"]) >>> ps.CategoricalIndex(c) CategoricalIndex(['a', 'b', 'c', 'a', 'b', 'c'], categories=['a', 'b', 'c'], ordered=False, dtype='category')
Ordered CategoricalIndex can have a min and max value.
>>> ci = ps.CategoricalIndex( ... ["a", "b", "c", "a", "b", "c"], ordered=True, categories=["c", "b", "a"] ... ) >>> ci CategoricalIndex(['a', 'b', 'c', 'a', 'b', 'c'], categories=['c', 'b', 'a'], ordered=True, dtype='category')
From a Series:
>>> s = ps.Series(["a", "b", "c", "a", "b", "c"], index=[10, 20, 30, 40, 50, 60]) >>> ps.CategoricalIndex(s) CategoricalIndex(['a', 'b', 'c', 'a', 'b', 'c'], categories=['a', 'b', 'c'], ordered=False, dtype='category')
From an Index:
>>> idx = ps.Index(["a", "b", "c", "a", "b", "c"]) >>> ps.CategoricalIndex(idx) CategoricalIndex(['a', 'b', 'c', 'a', 'b', 'c'], categories=['a', 'b', 'c'], ordered=False, dtype='category')
Methods
add_categories(new_categories[, inplace])
add_categories
Add new categories.
all(*args, **kwargs)
all
Return whether all elements are True.
any([axis])
any
Return whether any element is True.
append(other)
append
Append a collection of Index options together.
argmax()
argmax
Return a maximum argument indexer.
argmin()
argmin
Return a minimum argument indexer.
as_ordered([inplace])
as_ordered
Set the Categorical to be ordered.
as_unordered([inplace])
as_unordered
Set the Categorical to be unordered.
asof(label)
asof
Return the label from the index, or, if not present, the previous one.
astype(dtype)
astype
Cast a pandas-on-Spark object to a specified dtype dtype.
dtype
copy([name, deep])
copy
Make a copy of this object.
delete(loc)
delete
Make new Index with passed location(-s) deleted.
difference(other[, sort])
difference
Return a new Index with elements from the index that are not in other.
drop(labels)
drop
Make new Index with passed list of labels deleted.
drop_duplicates()
drop_duplicates
Return Index with duplicate values removed.
droplevel(level)
droplevel
Return index with requested level(s) removed.
dropna()
dropna
Return Index or MultiIndex without NA/NaN values
equals(other)
equals
Determine if two Index objects contain the same elements.
factorize([sort, na_sentinel])
factorize
Encode the object as an enumerated type or categorical variable.
fillna(value)
fillna
Fill NA/NaN values with the specified value.
get_level_values(level)
get_level_values
Return Index if a valid level is given.
holds_integer()
holds_integer
Whether the type is an integer type.
identical(other)
identical
Similar to equals, but check that other comparable attributes are also equal.
insert(loc, item)
insert
Make new Index inserting new item at location.
intersection(other)
intersection
Form the intersection of two Index objects.
is_boolean()
is_boolean
Return if the current index type is a boolean type.
is_categorical()
is_categorical
Return if the current index type is a categorical type.
is_floating()
is_floating
Return if the current index type is a floating type.
is_integer()
is_integer
Return if the current index type is a integer type.
is_interval()
is_interval
Return if the current index type is an interval type.
is_numeric()
is_numeric
Return if the current index type is a numeric type.
is_object()
is_object
Return if the current index type is a object type.
is_type_compatible(kind)
is_type_compatible
Whether the index type is compatible with the provided type.
isin(values)
isin
Check whether values are contained in Series or Index.
isna()
isna
Detect existing (non-missing) values.
isnull()
isnull
item()
item
Return the first element of the underlying data as a python scalar.
map(mapper)
map
Map values using input correspondence (a dict, Series, or function).
max()
max
Return the maximum value of the Index.
min()
min
Return the minimum value of the Index.
notna()
notna
notnull()
notnull
nunique([dropna, approx, rsd])
nunique
Return number of unique elements in the object.
remove_categories(removals[, inplace])
remove_categories
Remove the specified categories.
remove_unused_categories([inplace])
remove_unused_categories
Remove categories which are not used.
rename(name[, inplace])
rename
Alter Index or MultiIndex name.
rename_categories(new_categories[, inplace])
rename_categories
Rename categories.
reorder_categories(new_categories[, …])
reorder_categories
Reorder categories as specified in new_categories.
repeat(repeats)
repeat
Repeat elements of a Index/MultiIndex.
set_categories(new_categories[, ordered, …])
set_categories
Set the categories to the specified new_categories.
set_names(names[, level, inplace])
set_names
Set Index or MultiIndex name.
shift([periods, fill_value])
shift
Shift Series/Index by desired number of periods.
sort(*args, **kwargs)
sort
Use sort_values instead.
sort_values([ascending])
sort_values
Return a sorted copy of the index.
symmetric_difference(other[, result_name, sort])
symmetric_difference
Compute the symmetric difference of two Index objects.
take(indices)
take
Return the elements in the given positional indices along an axis.
to_frame([index, name])
to_frame
Create a DataFrame with a column containing the Index.
to_list()
to_list
Return a list of the values.
to_numpy([dtype, copy])
to_numpy
A NumPy ndarray representing the values in this Index or MultiIndex.
to_pandas()
to_pandas
Return a pandas Index.
to_series([name])
to_series
Create a Series with both index and values equal to the index keys useful with map for returning an indexer based on an index.
tolist()
tolist
transpose()
transpose
Return the transpose, For index, It will be index itself.
union(other[, sort])
union
Form the union of two Index objects.
unique([level])
unique
Return unique values in the index.
value_counts([normalize, sort, ascending, …])
value_counts
Return a Series containing counts of unique values.
view()
view
this is defined as a copy with the same identity
Attributes
T
asi8
Integer representation of the values.
categories
The categories of this categorical.
codes
The category codes of this categorical.
Return the dtype object of the underlying data.
empty
Returns true if the current object is empty.
has_duplicates
If index has duplicates, return True, otherwise False.
hasnans
Return True if it has any missing values.
inferred_type
Return a string of the type inferred from the values.
is_all_dates
Return if all data types of the index are datetime.
is_monotonic
Return boolean if values in the object are monotonically increasing.
is_monotonic_decreasing
Return boolean if values in the object are monotonically decreasing.
is_monotonic_increasing
is_unique
Return if the index has unique values.
name
Return name of the Index.
names
Return names of the Index.
ndim
Return an int representing the number of array dimensions.
nlevels
Number of levels in Index & MultiIndex.
ordered
Whether the categories have an ordered relationship.
shape
Return a tuple of the shape of the underlying data.
size
Return an int representing the number of elements in this object.
values
Return an array representing the data in the Index.