pandas.PeriodIndex#

class pandas.PeriodIndex(data=None, freq=None, dtype=None, copy=False, name=None)[source]#

Immutable ndarray holding ordinal values indicating regular periods in time.

Index keys are boxed to Period objects which carries the metadata (eg, frequency information).

Parameters:
dataarray-like (1d int np.ndarray or PeriodArray), optional

Optional period-like data to construct index with.

freqstr or period object, optional

One of pandas period strings or corresponding objects.

dtypestr or PeriodDtype, default None

A dtype from which to extract a freq.

copybool

Make a copy of input ndarray.

namestr, default None

Name of the resulting PeriodIndex.

Attributes

day

The days of the period.

dayofweek

The day of the week with Monday=0, Sunday=6.

day_of_week

The day of the week with Monday=0, Sunday=6.

dayofyear

The ordinal day of the year.

day_of_year

The ordinal day of the year.

days_in_month

The number of days in the month.

daysinmonth

The number of days in the month.

end_time

Get the Timestamp for the end of the period.

freq

Return the frequency object if it is set, otherwise None.

freqstr

Return the frequency object as a string if it's set, otherwise None.

hour

The hour of the period.

is_leap_year

Logical indicating if the date belongs to a leap year.

minute

The minute of the period.

month

The month as January=1, December=12.

quarter

The quarter of the date.

qyear

Fiscal year the Period lies in according to its starting-quarter.

second

The second of the period.

start_time

Get the Timestamp for the start of the period.

week

The week ordinal of the year.

weekday

The day of the week with Monday=0, Sunday=6.

weekofyear

The week ordinal of the year.

year

The year of the period.

Methods

asfreq([freq, how])

Convert the PeriodArray to the specified frequency freq.

strftime(date_format)

Convert to Index using specified date_format.

to_timestamp([freq, how])

Cast to DatetimeArray/Index.

from_fields(*[, year, quarter, month, day, ...])

Construct a PeriodIndex from fields (year, month, day, etc.).

from_ordinals(ordinals, *, freq[, name])

Construct a PeriodIndex from ordinals.

Raises:
ValueError

Passing the parameter data as a list without specifying either freq or dtype will raise a ValueError: “freq not specified and cannot be inferred”

See also

Index

The base pandas Index type.

Period

Represents a period of time.

DatetimeIndex

Index with datetime64 data.

TimedeltaIndex

Index of timedelta64 data.

period_range

Create a fixed-frequency PeriodIndex.

Examples

>>> idx = pd.PeriodIndex(data=["2000Q1", "2002Q3"], freq="Q")
>>> idx
PeriodIndex(['2000Q1', '2002Q3'], dtype='period[Q-DEC]')