GMc
GMc

Reputation: 199

Slice index Dask dataframe

Is there a simple way to slice a Dask dataframe index:

Something along these lines in Pandas?

index_element = df.index[-1]

Upvotes: 0

Views: 199

Answers (1)

Ray Bell
Ray Bell

Reputation: 1618

What are you after?

Doing .index[i] on a dask dataframe will give

import dask.dataframe as dd
df = dd.demo.make_timeseries(
    start="2000-01-01",
    end="2000-01-03",
    dtypes={"id": int, "z": int},
    freq="1h",
    partition_freq="24h",
)

df.index[-1]

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-7-d70d3c1197c1> in <module>
----> 1 df.index[-1]

~/miniconda/envs/main/lib/python3.8/site-packages/dask/dataframe/core.py in __getitem__(self, key)
   3172             graph = HighLevelGraph.from_collections(name, dsk, dependencies=[self, key])
   3173             return Series(graph, name, self._meta, self.divisions)
-> 3174         raise NotImplementedError(
   3175             "Series getitem in only supported for other series objects "
   3176             "with matching partition structure"

NotImplementedError: Series getitem in only supported for other series objects with matching partition structure

If you are after the index of the final row you could do:

df.tail(1).index

gives

DatetimeIndex(['2000-01-02 23:00:00'], dtype='datetime64[ns]', name='timestamp', freq='H')

Upvotes: 1

Related Questions