pandas.DatetimeIndex#

class pandas.DatetimeIndex(data=None, freq=<no_default>, tz=<no_default>, ambiguous='raise', dayfirst=False, yearfirst=False, dtype=None, copy=None, name=None)[source]#

Immutable ndarray-like of datetime64 data.

Represented internally as int64, and which can be boxed to Timestamp objects that are subclasses of datetime and carry metadata.

Changed in version 2.0.0: The various numeric date/time attributes (day, month, year etc.) now have dtype int32. Previously they had dtype int64.

Parameters:
dataarray-like (1-dimensional)

Datetime-like data to construct index with.

freqstr or pandas offset object, optional

One of pandas date offset strings or corresponding objects. The string ‘infer’ can be passed in order to set the frequency of the index as the inferred frequency upon creation.

tzzoneinfo.ZoneInfo, pytz.timezone, dateutil.tz.tzfile, datetime.tzinfo or str

Set the Timezone of the data.

ambiguous‘infer’, bool-ndarray, ‘NaT’, default ‘raise’

When clocks moved backward due to DST, ambiguous times may arise. For example in Central European Time (UTC+01), when going from 03:00 DST to 02:00 non-DST, 02:30:00 local time occurs both at 00:30:00 UTC and at 01:30:00 UTC. In such a situation, the ambiguous parameter dictates how ambiguous times should be handled.

  • ‘infer’ will attempt to infer fall dst-transition hours based on order

  • bool-ndarray where True signifies a DST time, False signifies a non-DST time (note that this flag is only applicable for ambiguous times)

  • ‘NaT’ will return NaT where there are ambiguous times

  • ‘raise’ will raise a ValueError if there are ambiguous times.

dayfirstbool, default False

If True, parse dates in data with the day first order.

yearfirstbool, default False

If True parse dates in data with the year first order.

dtypenumpy.dtype or DatetimeTZDtype or str, default None

Note that the only NumPy dtypes allowed are ‘datetime64[ns]’, ‘datetime64[us]’, ‘datetime64[ms]’, ‘datetime64[s]’.

copybool, default None

Whether to copy input data, only relevant for array, Series, and Index inputs (for other input, e.g. a list, a new array is created anyway). Defaults to True for array input and False for Index/Series. Set to False to avoid copying array input at your own risk (if you know the input data won’t be modified elsewhere). Set to True to force copying Series/Index up front.

namelabel, default None

Name to be stored in the index.

Attributes

date

Returns numpy array of python datetime.date objects.

time

Returns numpy array of datetime.time objects.

timetz

Returns numpy array of datetime.time objects with timezones.

tz

Return the timezone.

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.

inferred_freq

Return the inferred frequency of the index.

year

month

day

hour

minute

second

microsecond

nanosecond

dayofyear

day_of_year

dayofweek

day_of_week

weekday

quarter

is_month_start

is_month_end

is_quarter_start

is_quarter_end

is_year_start

is_year_end

is_leap_year

Methods

normalize()

Convert times to midnight.

strftime(date_format)

Convert to Index using specified date_format.

snap([freq])

Snap time stamps to nearest occurring frequency.

tz_convert(tz)

Convert tz-aware Datetime Array/Index from one time zone to another.

tz_localize(tz[, ambiguous, nonexistent])

Localize tz-naive Datetime Array/Index to tz-aware Datetime Array/Index.

round(freq[, ambiguous, nonexistent])

Perform round operation on the data to the specified freq.

floor(freq[, ambiguous, nonexistent])

Perform floor operation on the data to the specified freq.

ceil(freq[, ambiguous, nonexistent])

Perform ceil operation on the data to the specified freq.

to_period([freq])

Cast to PeriodArray/PeriodIndex at a particular frequency.

to_pydatetime()

Return an ndarray of datetime.datetime objects.

to_series([index, name])

Create a Series with both index and values equal to the index keys.

to_frame([index, name])

Create a DataFrame with a column containing the Index.

to_julian_date()

Convert Timestamp to a Julian Date.

month_name([locale])

Return the month names with specified locale.

day_name([locale])

Return the day names with specified locale.

mean(*[, skipna, axis])

Return the mean value of the Array.

std([axis, dtype, out, ddof, keepdims, skipna])

Return sample standard deviation over requested axis.

See also

Index

The base pandas Index type.

TimedeltaIndex

Index of timedelta64 data.

PeriodIndex

Index of Period data.

to_datetime

Convert argument to datetime.

date_range

Create a fixed-frequency DatetimeIndex.

Notes

To learn more about the frequency strings, please see this link.

Examples

>>> idx = pd.DatetimeIndex(["1/1/2020 10:00:00+00:00", "2/1/2020 11:00:00+00:00"])
>>> idx
DatetimeIndex(['2020-01-01 10:00:00+00:00', '2020-02-01 11:00:00+00:00'],
dtype='datetime64[us, UTC]', freq=None)