pandas.Series#
- class pandas.Series(data=None, index=None, dtype=None, name=None, copy=None)[source]#
One-dimensional ndarray with axis labels (including time series).
Labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Statistical methods from ndarray have been overridden to automatically exclude missing data (currently represented as NaN).
Operations between Series (+, -, /, *, **) align values based on their associated index values– they need not be the same length. The result index will be the sorted union of the two indexes.
- Parameters:
- dataarray-like, Iterable, dict, or scalar value
Contains data stored in Series. If data is a dict, argument order is maintained. Unordered sets are not supported.
- indexarray-like or Index (1d)
Values must be hashable and have the same length as data. Non-unique index values are allowed. Will default to RangeIndex (0, 1, 2, …, n) if not provided. If data is dict-like and index is None, then the keys in the data are used as the index. If the index is not None, the resulting Series is reindexed with the index values.
- dtypestr, numpy.dtype, or ExtensionDtype, optional
Data type for the output Series. If not specified, this will be inferred from data. See the user guide for more usages. If
data
is Series then is ignored.- nameHashable, default None
The name to give to the Series.
- copybool, default False
Copy input data. Only affects Series or 1d ndarray input. See examples.
See also
Notes
Please reference the User Guide for more information.
Examples
Constructing Series from a dictionary with an Index specified
>>> d = {"a": 1, "b": 2, "c": 3} >>> ser = pd.Series(data=d, index=["a", "b", "c"]) >>> ser a 1 b 2 c 3 dtype: int64
The keys of the dictionary match with the Index values, hence the Index values have no effect.
>>> d = {"a": 1, "b": 2, "c": 3} >>> ser = pd.Series(data=d, index=["x", "y", "z"]) >>> ser x NaN y NaN z NaN dtype: float64
Note that the Index is first built with the keys from the dictionary. After this the Series is reindexed with the given Index values, hence we get all NaN as a result.
Constructing Series from a list with copy=False.
>>> r = [1, 2] >>> ser = pd.Series(r, copy=False) >>> ser.iloc[0] = 999 >>> r [1, 2] >>> ser 0 999 1 2 dtype: int64
Due to input data type the Series has a copy of the original data even though copy=False, so the data is unchanged.
Constructing Series from a 1d ndarray with copy=False.
>>> r = np.array([1, 2]) >>> ser = pd.Series(r, copy=False) >>> ser.iloc[0] = 999 >>> r array([999, 2]) >>> ser 0 999 1 2 dtype: int64
Due to input data type the Series has a view on the original data, so the data is changed as well.
Attributes
Return the transpose, which is by definition self.
The ExtensionArray of the data backing this Series or Index.
Access a single value for a row/column label pair.
Dictionary of global attributes of this dataset.
axes
Return a list of the row axis labels.
Return the dtype object of the underlying data.
Return the dtype object of the underlying data.
Indicator whether Index is empty.
Get the properties associated with this pandas object.
Return True if there are any NaNs.
Access a single value for a row/column pair by integer position.
Purely integer-location based indexing for selection by position.
The index (axis labels) of the Series.
Return True if values in the object are monotonically decreasing.
Return True if values in the object are monotonically increasing.
Return True if values in the object are unique.
Access a group of rows and columns by label(s) or a boolean array.
Return the name of the Series.
Return the number of bytes in the underlying data.
Number of dimensions of the underlying data, by definition 1.
Return a tuple of the shape of the underlying data.
Return the number of elements in the underlying data.
Return Series as ndarray or ndarray-like depending on the dtype.
Methods
abs
()Return a Series/DataFrame with absolute numeric value of each element.
add
(other[, level, fill_value, axis])Return Addition of series and other, element-wise (binary operator add).
add_prefix
(prefix[, axis])Prefix labels with string prefix.
add_suffix
(suffix[, axis])Suffix labels with string suffix.
agg
([func, axis])Aggregate using one or more operations over the specified axis.
aggregate
([func, axis])Aggregate using one or more operations over the specified axis.
align
(other[, join, axis, level, copy, ...])Align two objects on their axes with the specified join method.
all
(*[, axis, bool_only, skipna])Return whether all elements are True, potentially over an axis.
any
(*[, axis, bool_only, skipna])Return whether any element is True, potentially over an axis.
apply
(func[, args, by_row])Invoke function on values of Series.
argmax
([axis, skipna])Return int position of the largest value in the Series.
argmin
([axis, skipna])Return int position of the smallest value in the Series.
argsort
([axis, kind, order, stable])Return the integer indices that would sort the Series values.
asfreq
(freq[, method, how, normalize, ...])Convert time series to specified frequency.
asof
(where[, subset])Return the last row(s) without any NaNs before where.
astype
(dtype[, copy, errors])Cast a pandas object to a specified dtype
dtype
.at_time
(time[, asof, axis])Select values at particular time of day (e.g., 9:30AM).
autocorr
([lag])Compute the lag-N autocorrelation.
between
(left, right[, inclusive])Return boolean Series equivalent to left <= series <= right.
between_time
(start_time, end_time[, ...])Select values between particular times of the day (e.g., 9:00-9:30 AM).
bfill
(*[, axis, inplace, limit, limit_area])Fill NA/NaN values by using the next valid observation to fill the gap.
case_when
(caselist)Replace values where the conditions are True.
clip
([lower, upper, axis, inplace])Trim values at input threshold(s).
combine
(other, func[, fill_value])Combine the Series with a Series or scalar according to func.
combine_first
(other)Update null elements with value in the same location in 'other'.
compare
(other[, align_axis, keep_shape, ...])Compare to another Series and show the differences.
convert_dtypes
([infer_objects, ...])Convert columns from numpy dtypes to the best dtypes that support
pd.NA
.copy
([deep])Make a copy of this object's indices and data.
corr
(other[, method, min_periods])Compute correlation with other Series, excluding missing values.
count
()Return number of non-NA/null observations in the Series.
cov
(other[, min_periods, ddof])Compute covariance with Series, excluding missing values.
cummax
([axis, skipna])Return cumulative maximum over a DataFrame or Series axis.
cummin
([axis, skipna])Return cumulative minimum over a DataFrame or Series axis.
cumprod
([axis, skipna])Return cumulative product over a DataFrame or Series axis.
cumsum
([axis, skipna])Return cumulative sum over a DataFrame or Series axis.
describe
([percentiles, include, exclude])Generate descriptive statistics.
diff
([periods])First discrete difference of element.
div
(other[, level, fill_value, axis])Return Floating division of series and other, element-wise (binary operator truediv).
divide
(other[, level, fill_value, axis])Return Floating division of series and other, element-wise (binary operator truediv).
divmod
(other[, level, fill_value, axis])Return Integer division and modulo of series and other, element-wise (binary operator divmod).
dot
(other)Compute the dot product between the Series and the columns of other.
drop
([labels, axis, index, columns, level, ...])Return Series with specified index labels removed.
drop_duplicates
(*[, keep, inplace, ignore_index])Return Series with duplicate values removed.
droplevel
(level[, axis])Return Series/DataFrame with requested index / column level(s) removed.
dropna
(*[, axis, inplace, how, ignore_index])Return a new Series with missing values removed.
duplicated
([keep])Indicate duplicate Series values.
eq
(other[, level, fill_value, axis])Return Equal to of series and other, element-wise (binary operator eq).
equals
(other)Test whether two objects contain the same elements.
ewm
([com, span, halflife, alpha, ...])Provide exponentially weighted (EW) calculations.
expanding
([min_periods, method])Provide expanding window calculations.
explode
([ignore_index])Transform each element of a list-like to a row.
factorize
([sort, use_na_sentinel])Encode the object as an enumerated type or categorical variable.
ffill
(*[, axis, inplace, limit, limit_area])Fill NA/NaN values by propagating the last valid observation to next valid.
fillna
(value, *[, axis, inplace, limit])Fill NA/NaN values with value.
filter
([items, like, regex, axis])Subset the DataFrame or Series according to the specified index labels.
Return index for first non-missing value or None, if no value is found.
floordiv
(other[, level, fill_value, axis])Return Integer division of series and other, element-wise (binary operator floordiv).
ge
(other[, level, fill_value, axis])Return Greater than or equal to of series and other, element-wise (binary operator ge).
get
(key[, default])Get item from object for given key (ex: DataFrame column).
groupby
([by, level, as_index, sort, ...])Group Series using a mapper or by a Series of columns.
gt
(other[, level, fill_value, axis])Return Greater than of series and other, element-wise (binary operator gt).
head
([n])Return the first n rows.
hist
([by, ax, grid, xlabelsize, xrot, ...])Draw histogram of the input series using matplotlib.
idxmax
([axis, skipna])Return the row label of the maximum value.
idxmin
([axis, skipna])Return the row label of the minimum value.
infer_objects
([copy])Attempt to infer better dtypes for object columns.
info
([verbose, buf, max_cols, memory_usage, ...])Print a concise summary of a Series.
interpolate
([method, axis, limit, inplace, ...])Fill NaN values using an interpolation method.
isin
(values)Whether elements in Series are contained in values.
isna
()Detect missing values.
isnull
()Series.isnull is an alias for Series.isna.
item
()Return the first element of the underlying data as a Python scalar.
items
()Lazily iterate over (index, value) tuples.
keys
()Return alias for index.
kurt
(*[, axis, skipna, numeric_only])Return unbiased kurtosis over requested axis.
kurtosis
(*[, axis, skipna, numeric_only])Return unbiased kurtosis over requested axis.
Return index for last non-missing value or None, if no value is found.
le
(other[, level, fill_value, axis])Return Less than or equal to of series and other, element-wise (binary operator le).
lt
(other[, level, fill_value, axis])Return Greater than of series and other, element-wise (binary operator lt).
map
(arg[, na_action])Map values of Series according to an input mapping or function.
mask
(cond[, other, inplace, axis, level])Replace values where the condition is True.
max
(*[, axis, skipna, numeric_only])Return the maximum of the values over the requested axis.
mean
(*[, axis, skipna, numeric_only])Return the mean of the values over the requested axis.
median
(*[, axis, skipna, numeric_only])Return the median of the values over the requested axis.
memory_usage
([index, deep])Return the memory usage of the Series.
min
(*[, axis, skipna, numeric_only])Return the minimum of the values over the requested axis.
mod
(other[, level, fill_value, axis])Return Modulo of series and other, element-wise (binary operator mod).
mode
([dropna])Return the mode(s) of the Series.
mul
(other[, level, fill_value, axis])Return Multiplication of series and other, element-wise (binary operator mul).
multiply
(other[, level, fill_value, axis])Return Multiplication of series and other, element-wise (binary operator mul).
ne
(other[, level, fill_value, axis])Return Not equal to of series and other, element-wise (binary operator ne).
nlargest
([n, keep])Return the largest n elements.
notna
()Detect existing (non-missing) values.
notnull
()Series.notnull is an alias for Series.notna.
nsmallest
([n, keep])Return the smallest n elements.
nunique
([dropna])Return number of unique elements in the object.
pct_change
([periods, fill_method, freq])Fractional change between the current and a prior element.
pipe
(func, *args, **kwargs)Apply chainable functions that expect Series or DataFrames.
pop
(item)Return item and drops from series.
pow
(other[, level, fill_value, axis])Return Exponential power of series and other, element-wise (binary operator pow).
prod
(*[, axis, skipna, numeric_only, min_count])Return the product of the values over the requested axis.
product
(*[, axis, skipna, numeric_only, ...])Return the product of the values over the requested axis.
quantile
([q, interpolation])Return value at the given quantile.
radd
(other[, level, fill_value, axis])Return Addition of series and other, element-wise (binary operator radd).
rank
([axis, method, numeric_only, ...])Compute numerical data ranks (1 through n) along axis.
rdiv
(other[, level, fill_value, axis])Return Floating division of series and other, element-wise (binary operator rtruediv).
rdivmod
(other[, level, fill_value, axis])Return Integer division and modulo of series and other, element-wise (binary operator rdivmod).
reindex
([index, axis, method, copy, level, ...])Conform Series to new index with optional filling logic.
reindex_like
(other[, method, copy, limit, ...])Return an object with matching indices as other object.
rename
([index, axis, copy, inplace, level, ...])Alter Series index labels or name.
rename_axis
([mapper, index, axis, copy, inplace])Set the name of the axis for the index.
reorder_levels
(order)Rearrange index levels using input order.
repeat
(repeats[, axis])Repeat elements of a Series.
replace
([to_replace, value, inplace, regex])Replace values given in to_replace with value.
resample
(rule[, closed, label, convention, ...])Resample time-series data.
reset_index
([level, drop, name, inplace, ...])Generate a new DataFrame or Series with the index reset.
rfloordiv
(other[, level, fill_value, axis])Return Integer division of series and other, element-wise (binary operator rfloordiv).
rmod
(other[, level, fill_value, axis])Return Modulo of series and other, element-wise (binary operator rmod).
rmul
(other[, level, fill_value, axis])Return Multiplication of series and other, element-wise (binary operator rmul).
rolling
(window[, min_periods, center, ...])Provide rolling window calculations.
round
([decimals])Round each value in a Series to the given number of decimals.
rpow
(other[, level, fill_value, axis])Return Exponential power of series and other, element-wise (binary operator rpow).
rsub
(other[, level, fill_value, axis])Return Subtraction of series and other, element-wise (binary operator rsub).
rtruediv
(other[, level, fill_value, axis])Return Floating division of series and other, element-wise (binary operator rtruediv).
sample
([n, frac, replace, weights, ...])Return a random sample of items from an axis of object.
searchsorted
(value[, side, sorter])Find indices where elements should be inserted to maintain order.
sem
(*[, axis, skipna, ddof, numeric_only])Return unbiased standard error of the mean over requested axis.
set_axis
(labels, *[, axis, copy])Assign desired index to given axis.
set_flags
(*[, copy, allows_duplicate_labels])Return a new object with updated flags.
shift
([periods, freq, axis, fill_value, suffix])Shift index by desired number of periods with an optional time freq.
skew
(*[, axis, skipna, numeric_only])Return unbiased skew over requested axis.
sort_index
(*[, axis, level, ascending, ...])Sort Series by index labels.
sort_values
(*[, axis, ascending, inplace, ...])Sort by the values.
squeeze
([axis])Squeeze 1 dimensional axis objects into scalars.
std
(*[, axis, skipna, ddof, numeric_only])Return sample standard deviation over requested axis.
sub
(other[, level, fill_value, axis])Return Subtraction of series and other, element-wise (binary operator sub).
subtract
(other[, level, fill_value, axis])Return Subtraction of series and other, element-wise (binary operator sub).
sum
(*[, axis, skipna, numeric_only, min_count])Return the sum of the values over the requested axis.
swaplevel
([i, j, copy])Swap levels i and j in a
MultiIndex
.tail
([n])Return the last n rows.
take
(indices[, axis])Return the elements in the given positional indices along an axis.
to_clipboard
(*[, excel, sep])Copy object to the system clipboard.
to_csv
([path_or_buf, sep, na_rep, ...])Write object to a comma-separated values (csv) file.
to_dict
(*[, into])Convert Series to {label -> value} dict or dict-like object.
to_excel
(excel_writer, *[, sheet_name, ...])Write object to an Excel sheet.
to_frame
([name])Convert Series to DataFrame.
to_hdf
(path_or_buf, *, key[, mode, ...])Write the contained data to an HDF5 file using HDFStore.
to_json
([path_or_buf, orient, date_format, ...])Convert the object to a JSON string.
to_latex
([buf, columns, header, index, ...])Render object to a LaTeX tabular, longtable, or nested table.
to_list
()Return a list of the values.
to_markdown
([buf, mode, index, storage_options])Print Series in Markdown-friendly format.
to_numpy
([dtype, copy, na_value])A NumPy ndarray representing the values in this Series or Index.
to_period
([freq, copy])Convert Series from DatetimeIndex to PeriodIndex.
to_pickle
(path, *[, compression, protocol, ...])Pickle (serialize) object to file.
to_sql
(name, con, *[, schema, if_exists, ...])Write records stored in a DataFrame to a SQL database.
to_string
([buf, na_rep, float_format, ...])Render a string representation of the Series.
to_timestamp
([freq, how, copy])Cast to DatetimeIndex of Timestamps, at beginning of period.
Return an xarray object from the pandas object.
tolist
()Return a list of the values.
transform
(func[, axis])Call
func
on self producing a Series with the same axis shape as self.transpose
(*args, **kwargs)Return the transpose, which is by definition self.
truediv
(other[, level, fill_value, axis])Return Floating division of series and other, element-wise (binary operator truediv).
truncate
([before, after, axis, copy])Truncate a Series or DataFrame before and after some index value.
tz_convert
(tz[, axis, level, copy])Convert tz-aware axis to target time zone.
tz_localize
(tz[, axis, level, copy, ...])Localize time zone naive index of a Series or DataFrame to target time zone.
unique
()Return unique values of Series object.
unstack
([level, fill_value, sort])Unstack, also known as pivot, Series with MultiIndex to produce DataFrame.
update
(other)Modify Series in place using values from passed Series.
value_counts
([normalize, sort, ascending, ...])Return a Series containing counts of unique values.
var
(*[, axis, skipna, ddof, numeric_only])Return unbiased variance over requested axis.
where
(cond[, other, inplace, axis, level])Replace values where the condition is False.
xs
(key[, axis, level, drop_level])Return cross-section from the Series/DataFrame.