Skip to content

ENH: add Series.set_index #27058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue Jun 26, 2019 · 3 comments
Closed

ENH: add Series.set_index #27058

ghost opened this issue Jun 26, 2019 · 3 comments

Comments

@ghost
Copy link

ghost commented Jun 26, 2019

Related #21684

update: use set_axis.

import pandas as pd
 
dates=[
"2001-01-01 23:50",
"2001-01-02 14:00", 
"2001-01-03 00:08"] 
 
ser=pd.Series(range(3),pd.DatetimeIndex(dates))

sometimes when writing a sequence of operation, you want to replace the index of the object you're working on. Say, quantizing the dates. I can't find the .method way of doing
ser.index= expr
So that I can't write something like

(ser
      .replace_index(ser.index.floor("D"))    # < this doesn't exist
      .rolling("5d")
       .sum()

Things that don't solve this:

  • reindex is aligning, so if your new index permutes the indexes, it isn't suitable.
  • set_index takes a column name, not an expression, and isn't available on series. update works for frames, though you must provide an arraylike, not a listlike.
  • reindex_like expects a PandasObject instead of a listlike, and is aligning
  • reset_index does not take a values argument
In [1]: [_ for _ in dir(ser) if 'index' in _ and not _.startswith("_")]
Out [1]: 
['first_valid_index',
 'index',
 'last_valid_index',
 'reindex',
 'reindex_axis',
 'reindex_like',
 'reset_index',
 'sort_index']
@ghost ghost changed the title ENH: pipe-style way to assign a new index ENH: Chaining method for assigning a new index Jun 26, 2019
@Liam3851
Copy link
Contributor

You can do this in a chain now by converting the Series to DataFrame, using set_index, and then converting back to Series, since DataFrame.set_index accepts an arraylike to use as the new index:

In [21]: ser.to_frame() \
    ...:    .set_index(ser.index.floor('D')) \
    ...:    .iloc[:, 0] \
    ...:    .rolling('5D') \
    ...:    .sum()
    ...:
Out[21]:
2001-01-01    0.0
2001-01-02    1.0
2001-01-03    3.0
Name: 0, dtype: float64

Perhaps there is a case for set_index on Series that would provide the arraylike functionality of DataFrame.set_index without requiring conversion to/from DataFrame?

@ghost
Copy link
Author

ghost commented Jun 26, 2019

Thanks, you're right. I updated the OP.

@ghost ghost changed the title ENH: Chaining method for assigning a new index ENH: add Series.set_index Jul 8, 2019
@ghost ghost mentioned this issue Jul 21, 2019
4 tasks
@ghost
Copy link
Author

ghost commented Jul 22, 2019

It turns out that the set_axis method has existed since 0.21 for doing this, but it seems it's not very well-known. Closing for now, as this is a dupe of #21684 and there are larger issues there, with much previous discussion.

@ghost ghost closed this as completed Jul 22, 2019
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant