ENH: Add `pd.Series.stack() -> pd.MultiIndex`

This issue has been created since 2022-11-17.

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

I wish to be able to take a series that has a regular or a multi-index. and add the data of the series as the highest level in a MultiIndex. Right now it requires annoying and inefficient workarounds.

Feature Description

I wish I could have a variant of the pd.DataFrame.stack() function, but for pd.Series in the sense that it always outputs a pd.MultiIndex. The function should take the index of the the series regardless of it being a flat or a multi-index and just add the data as the top level.
On the other direction, a pd.MultiIndex should have an unstack() method that does the exact inverse (takes a multi-index, removes its highest level and turns it into the data of the series).

Not that this way: while not isinstance(df, pd.Index): df = df.stack() would be (possibly inefficiently) the same as pd.MultiIndex.from_frame(df).

Alternative Solutions

  1. The simplest (yet most expansive option) is to fully-unstack the series into a data-frame, and than use pd.MultiIndex.from_frame.
  2. Another option is to cast the series data as categorical, and then use the codes and categories to construct a new multi-index (along with the codes and categories of the original series' index).
  3. A third option is to use pd.MultiIndex.from_frame(series.reset_index()). (again - possibly inefficient)

Additional Context

No response

samukweku wrote this answer on 2022-11-17

@erezinman Kindly add examples (code) to illustrate your point

erezinman wrote this answer on 2022-11-18

@samukweku See below. Tell me if that's understood or you need anything more.

>>> df = pd.DataFrame([
...     [10, None, 0],
...     [None, None, 1],
...     [None, '132', 1]
... ], 
...     columns=['one', 'two', 'three'],
...     index=[10, 20, 30]
... )

#           10     20     30
# one     True  False  False
# two    False  False   True
# three   True   True   True

>>> series = df.stack()
>>> series
# 10  one      10.0
#     three       0
# 20  three       1
# 30  two       132
#     three       1
# dtype: object

################################## NEW FROM HERE:

>>> idx = series.stack()  # Or some other name?
>>> idx
# MultiIndex([(10,   'one',  10.0),
#             (10, 'three',     0),
#             (20, 'three',     1),
#             (30,   'two', '132'),
#             (30, 'three',     1)],
#            names=['level_0', 'level_1', 0])

>>> idx2 = idx.swaplevel()
>>> idx2.unstack()  # Or some other name?
# level_0  0   
# 10       10.0      one
#          0       three
# 20       1       three
# 30       132       two
#          1       three
# Name: level_1, dtype: object
More Details About Repo
Owner Name pandas-dev
Repo Name pandas
Full Name pandas-dev/pandas
Language Python
Created Date 2010-08-24
Updated Date 2022-12-07
Star Count 36168
Watcher Count 1118
Fork Count 15472
Issue Count 3683

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
当使用vlc内核加载ass或者srt字幕时,无法正常读取tff字体文件,除非把字体文件封装进mkv文件中才能正常读取 0 2022-11-11 2022-11-26
Wikilink in Anki contains text that should be outside of the link. 0 2022-09-29 2022-11-20
OpenCore not working with use_graphics_for 7 2021-05-18 2022-03-27
Follow google design guidelines 3 2022-10-06 2022-12-05
gabwin and comp_window behave differently for g = {'gauss'} and g = 'gauss' 1 2020-01-13 2022-10-29
publish v0.6.0 on CRAN 3 2021-10-22 2022-12-06
make archived codes more accessible 0 2021-11-04 2022-01-02
Request: Query a spreadsheet with OLEDB 8 2021-10-27 2022-12-04
Add note that TLC isn't guaranteed to find shortest counter-example 1 2019-05-26 2022-10-03
Incorrect answer for LargestTwinPair exercise 0 2020-03-18 2022-10-03
Is there any possibility of keeping the in-app recorder? 1 2022-11-26 2022-11-29
最新版v2.0.9 无法访问自己的博客主页 1 2021-11-25 2022-11-03
Nil value error when loading plugin 1 2022-05-02 2022-11-21
zerosonesfun/composer-preview 1 2022-05-27 2022-11-20
Lightbox does not bind to duplicate element 1 2022-02-08 2022-11-11
ctest error : uvw_util faild 1 2021-07-17 2022-12-07
Humblebundle Navbar content loading issues. 0 2021-12-21 2022-09-15
💚 Overall project cleaned and gitmoji-friendly 0 2021-10-26 2022-02-04
Should the SPACE grok pattern match zero or more? 1 2019-06-20 2022-10-05
kafkajs holding one partition but not fetching from it 0 2022-10-07 2022-11-24
Documentation is misleading and incomplete 1 2022-09-13 2022-11-19
Group courses in categories to make topics and navigation more clear 0 2022-02-15 2022-12-05
Problem with instancing FieldUserValue 4 2022-09-21 2022-11-28
千钧一发之际赢得暂缓令,苹果App Store要反败为胜了? 0 2021-12-09 2022-10-31
DERP: avoid serving derps behind high level load balancers 4 2022-11-24 2022-11-22
Component Margin attribute generates wrong classes ! 2 2022-06-29 2022-11-01
[RFC #0093] Update process types in inspect-image output to support command slice 0 2021-08-18 2022-11-14
Error when POST file multipart/form-data 1 2022-11-17 2022-12-07
www.city.tokai.aichi.jp 0 2022-07-17 2022-10-11
Can the translation problem be improved? 0 2022-11-20 2022-11-28
removing deprecated commands in etcdctl 1 2022-03-14 2022-10-22
Bug: `eslint-disable` should accept `//` style comments too 1 2021-11-17 2022-11-22
Is there an alternative to the da fit app? 4 2021-06-19 2022-11-21
Differing versions of elasticsearch for client and server 3 2021-05-15 2022-11-16
@RequestAttribute in HTTP Client is being treated as a Query Parameter in 3.2.x 2 2022-01-06 2022-09-28
[11] Password Security - Reset password 9 2018-11-08 2022-11-19
Could not retrieve any SDL2 window info 3 2020-09-03 2022-11-19
ADF to Azure function using Azure Function linked service managed identity authentication fails 2 2021-10-14 2022-11-02
rsub fails test_noncontiguous_samples 1 2021-10-29 2022-11-03
Numeric representation of output heatmaps (Occlusion and LayerGradCAM) 4 2022-04-05 2022-11-27
Want to get the response time of each requests that are being sent during the simulation 1 2021-07-23 2022-11-04
Error when I import import 'package:network_to_file_image/network_to_file_image.dart'; 4 2020-04-20 2022-11-04
paddleocr十讲课程里面,文本识别的那节课里面没给数据集,程序运行不了了 3 2022-08-03 2022-11-12
Remove "in X hours ago" ?? 1 2020-08-19 2022-08-20
RawMessageBuilder can only use one message object 1 2021-01-06 2022-11-27
LostInvalidationTest test fail [API-1199] 4 2021-09-08 2022-10-08
Vagrant 2.2.17 Breaks Network Configuration 9 2021-07-08 2022-12-07
有考虑使用kryo5.X么 4.X最后更新时间是2018年 5.X目前已经更新到5.3.0了 更新的很活跃 功能也更好一些 4 2022-09-27 2022-10-15
Question about testing ABI 2 2021-09-19 2022-11-26
Could Not build Flutter Android Build : Received status code 502 from bintray 7 2021-12-14 2022-11-03