def strftime(self, date_format: str) -> Index: """ Convert to a string Index using specified date_format. Return an Index of formatted strings specified by date_format, which supports the same string format as the python standard library. Details of the string format can be found in python string format doc. Parameters ---------- date_format : str Date format string (e.g. "%%Y-%%m-%%d"). Returns ------- Index Index of formatted strings. See Also -------- normalize : Return series with times to midnight. round : Round the series to the specified freq. floor : Floor the series to the specified freq. Examples -------- >>> idx = ks.date_range(pd.Timestamp("2018-03-10 09:00"), periods=3, freq='s') >>> idx.strftime('%B %d, %Y, %r') # doctest: +NORMALIZE_WHITESPACE Index(['March 10, 2018, 09:00:00 AM', 'March 10, 2018, 09:00:01 AM', 'March 10, 2018, 09:00:02 AM'], dtype='object') """ return Index(self.to_series().dt.strftime(date_format))
def dayofweek(self) -> Index: """ The day of the week with Monday=0, Sunday=6. Return the day of the week. It is assumed the week starts on Monday, which is denoted by 0 and ends on Sunday which is denoted by 6. This method is available on both Series with datetime values (using the `dt` accessor) or DatetimeIndex. Returns ------- Series or Index Containing integers indicating the day number. See Also -------- Series.dt.dayofweek : Alias. Series.dt.weekday : Alias. Series.dt.day_name : Returns the name of the day of the week. Examples -------- >>> idx = ks.date_range('2016-12-31', '2017-01-08', freq='D') >>> idx.dayofweek Int64Index([5, 6, 0, 1, 2, 3, 4, 5, 6], dtype='int64') """ return Index(self.to_series().dt.dayofweek)
def __new__( cls, data=None, freq=_NoValue, normalize=False, closed=None, ambiguous="raise", dayfirst=False, yearfirst=False, dtype=None, copy=False, name=None, ): if not is_hashable(name): raise TypeError("Index.name must be a hashable type") if isinstance(data, (Series, Index)): if dtype is None: dtype = "datetime64[ns]" return Index(data, dtype=dtype, copy=copy, name=name) kwargs = dict( data=data, normalize=normalize, closed=closed, ambiguous=ambiguous, dayfirst=dayfirst, yearfirst=yearfirst, dtype=dtype, copy=copy, name=name, ) if freq is not _NoValue: kwargs["freq"] = freq return ks.from_pandas(pd.DatetimeIndex(**kwargs))
def __new__(cls, data=None, dtype=None, copy=False, name=None): if not is_hashable(name): raise TypeError("Index.name must be a hashable type") if isinstance(data, (Series, Index)): if dtype is None: dtype = "int64" return Index(data, dtype=dtype, copy=copy, name=name) return ks.from_pandas( pd.Int64Index(data=data, dtype=dtype, copy=copy, name=name))
def is_year_end(self) -> Index: """ Indicate whether the date is the last day of the year. Returns ------- Index Returns an Index with boolean values. See Also -------- is_year_start : Similar property indicating the start of the year. Examples -------- >>> idx = ks.date_range("2017-12-30", periods=3) >>> idx.is_year_end Index([False, True, False], dtype='object') """ return Index(self.to_series().dt.is_year_end)
def __new__(cls, data=None, categories=None, ordered=None, dtype=None, copy=False, name=None): if not is_hashable(name): raise TypeError("Index.name must be a hashable type") if isinstance(data, (Series, Index)): if dtype is None: dtype = "category" return Index(data, dtype=dtype, copy=copy, name=name) return ks.from_pandas( pd.CategoricalIndex(data=data, categories=categories, ordered=ordered, dtype=dtype, name=name))
def is_quarter_end(self) -> Index: """ Indicator for whether the date is the last day of a quarter. Returns ------- is_quarter_end : Index Returns an Index with boolean values. See Also -------- quarter : Return the quarter of the date. is_quarter_start : Similar property indicating the quarter start. Examples -------- >>> idx = ks.date_range('2017-03-30', periods=4) >>> idx.is_quarter_end Index([False, True, False, False], dtype='object') """ return Index(self.to_series().dt.is_quarter_end)
def is_leap_year(self) -> Index: """ Boolean indicator if the date belongs to a leap year. A leap year is a year, which has 366 days (instead of 365) including 29th of February as an intercalary day. Leap years are years which are multiples of four with the exception of years divisible by 100 but not by 400. Returns ------- Index Booleans indicating if dates belong to a leap year. Examples -------- >>> idx = ks.date_range("2012-01-01", "2015-01-01", freq="Y") >>> idx.is_leap_year Index([True, False, False], dtype='object') """ return Index(self.to_series().dt.is_leap_year)
def is_month_end(self) -> Index: """ Indicates whether the date is the last day of the month. Returns ------- Index Returns a Index with boolean values. See Also -------- is_month_start : Return a boolean indicating whether the date is the first day of the month. Examples -------- >>> idx = ks.date_range("2018-02-27", periods=3) >>> idx.is_month_end Index([False, True, False], dtype='object') """ return Index(self.to_series().dt.is_month_end)
def day_name(self, locale: Optional[str] = None) -> Index: """ Return the day names of the series with specified locale. Parameters ---------- locale : str, optional Locale determining the language in which to return the day name. Default is English locale. Returns ------- Index Index of day names. Examples -------- >>> idx = ks.date_range(start='2018-01-01', freq='D', periods=3) >>> idx.day_name() Index(['Monday', 'Tuesday', 'Wednesday'], dtype='object') """ return Index(self.to_series().dt.day_name(locale))
def month_name(self, locale: Optional[str] = None) -> Index: """ Return the month names of the DatetimeIndex with specified locale. Parameters ---------- locale : str, optional Locale determining the language in which to return the month name. Default is English locale. Returns ------- Index Index of month names. Examples -------- >>> idx = ks.date_range(start='2018-01', freq='M', periods=3) >>> idx.month_name() Index(['January', 'February', 'March'], dtype='object') """ return Index(self.to_series().dt.month_name(locale))
def quarter(self) -> Index: """ The quarter of the date. """ return Index(self.to_series().dt.quarter)
def daysinmonth(self) -> Index: """ The number of days in the month. """ return Index(self.to_series().dt.daysinmonth)
def days_in_month(self) -> Index: return Index(self.to_series().dt.days_in_month)
def month(self) -> Index: """ The month of the timestamp as January = 1 December = 12. """ return Index(self.to_series().dt.month)
def microsecond(self) -> Index: """ The microseconds of the datetime. """ return Index(self.to_series().dt.microsecond)
def minute(self) -> Index: """ The minutes of the datetime. """ return Index(self.to_series().dt.minute)
def week(self) -> Index: """ The week ordinal of the year. """ return Index(self.to_series().dt.week)
def year(self) -> Index: """ The year of the datetime. """ return Index(self.to_series().dt.year)
def dayofyear(self) -> Index: """ The ordinal day of the year. """ return Index(self.to_series().dt.dayofyear)
def insert(self, loc: int, item) -> Index: """ Make new MultiIndex inserting new item at location. Follows Python list.append semantics for negative values. Parameters ---------- loc : int item : object Returns ------- new_index : MultiIndex Examples -------- >>> kmidx = ks.MultiIndex.from_tuples([("a", "x"), ("b", "y"), ("c", "z")]) >>> kmidx.insert(3, ("h", "j")) # doctest: +SKIP MultiIndex([('a', 'x'), ('b', 'y'), ('c', 'z'), ('h', 'j')], ) For negative values >>> kmidx.insert(-2, ("h", "j")) # doctest: +SKIP MultiIndex([('a', 'x'), ('h', 'j'), ('b', 'y'), ('c', 'z')], ) """ length = len(self) if loc < 0: loc = loc + length if loc < 0: raise IndexError( "index {} is out of bounds for axis 0 with size {}".format( (loc - length), length)) else: if loc > length: raise IndexError( "index {} is out of bounds for axis 0 with size {}".format( loc, length)) index_name = self._internal.index_spark_column_names sdf_before = self.to_frame(name=index_name)[:loc].to_spark() sdf_middle = Index([item]).to_frame(name=index_name).to_spark() sdf_after = self.to_frame(name=index_name)[loc:].to_spark() sdf = sdf_before.union(sdf_middle).union(sdf_after) internal = InternalFrame( # TODO: dtypes? spark_frame=sdf, index_spark_columns=[ scol_for(sdf, col) for col in self._internal.index_spark_column_names ], index_names=self._internal.index_names, ) return DataFrame(internal).index
def day(self) -> Index: """ The days of the datetime. """ return Index(self.to_series().dt.day)
def weekofyear(self) -> Index: return Index(self.to_series().dt.weekofyear)
def weekday(self) -> Index: return Index(self.to_series().dt.weekday)
def hour(self) -> Index: """ The hours of the datetime. """ return Index(self.to_series().dt.hour)