簡體   English   中英

Pandas 中的字典錯誤?

[英]A Lexicographical Bug in Pandas?

出於好奇,請輕視這個問題:

我試圖查看 MultiIndex 中的切片如何工作時,我遇到了以下情況↓

# Simple MultiIndex Creation
index = pd.MultiIndex.from_product([['a', 'c', 'b'], [1, 2]])

# Making Series with that MultiIndex
data = pd.Series(np.random.randint(10, size=6), index=index)

返回:

a  1    5
   2    0
c  1    8
   2    6
b  1    6
   2    3
dtype: int32

請注意,索引不是按排序順序排列的,即。 a, c, b是在切片時導致我們想要的預期誤差的順序。

# When we do slicing
data.loc["a":"c"]

錯誤如:

UnsortedIndexError

----> 1 data.loc["a":"c"]
UnsortedIndexError: 'Key length (1) was greater than MultiIndex lexsort depth (0)'

這是預期的。 但是現在,在執行以下步驟后:

# Making a DataFrame
data = data.unstack()

# Redindexing - to unsort the indices like before
data = data.reindex(["a", "c", "b"])

# Which looks like 
   1  2
a  5  0
c  8  6
b  6  3

# Then again making series
data = data.stack()

# Reindex Again!
data = data.reindex(["a", "c", "b"], level=0)


# Which looks like before
a  1    5
   2    0
c  1    8
   2    6
b  1    6
   2    3
dtype: int32

問題

所以,現在的流程是: Series → Unstack → DataFrame → Stack → Series

現在,如果我像以前一樣進行切片(仍然使用未排序的索引),我們不會收到任何錯誤!

# The same slicing
data.loc["a":"c"]

沒有錯誤的結果:

a  1    5
   2    0
c  1    8
   2    6
dtype: int32

即使data.index.is_monotonicFalse 那為什么還要切片呢?

所以問題是:為什么? .

我希望你對這里的情況有所了解。 因為看,同一系列之前報錯, unstackstack操作之后沒有報錯。

那么這是一個錯誤,還是我在這里遺漏的一個新概念?

謝謝!
阿尤什 ∞ 沙阿

更新:我已經使用了data.reindex()以便再次取消排序。 請再看一遍。

2個數據幀之間的區別如下:

index = pd.MultiIndex.from_product([['a', 'c', 'b'], [1, 2]])

data = pd.Series(np.random.randint(10, size=6), index=index)

data2 = data.unstack().reindex(["a", "c", "b"]).stack()

>>> data.index.codes
FrozenList([[0, 0, 2, 2, 1, 1], [0, 1, 0, 1, 0, 1]])

>>> data2.index.codes
FrozenList([[0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1]])

即使你的兩個索引是相同的外觀(值),內部索引(代碼)也是不同的。

檢查MultiIndex 這種方法

        Create a new MultiIndex from the current to monotonically sorted
        items IN the levels. This does not actually make the entire MultiIndex
        monotonic, JUST the levels.

        The resulting MultiIndex will have the same outward
        appearance, meaning the same .values and ordering. It will also
        be .equals() to the original.

舊答案

# Making a DataFrame
data = data.unstack()

# Which looks like         # <- WRONG
   1  2                    #    1  2
a  5  0                    # a  8  0
c  8  6                    # b  4  1
b  6  3                    # c  7  6

# Then again making series
data = data.stack()

# Which looks like before  # <- WRONG
a  1    5                  # a  1    2
   2    0                  #    2    1
c  1    8                  # b  1    0
   2    6                  #    2    1
b  1    6                  # c  1    3
   2    3                  #    2    9
dtype: int32

如果要使用切片,則必須檢查索引是否單調:

# Simple MultiIndex Creation
index = pd.MultiIndex.from_product([['a', 'c', 'b'], [1, 2]])

# Making Series with that MultiIndex
data = pd.Series(np.random.randint(10, size=6), index=index)

>>> data.index.is_monotonic
False

>>> data.unstack().stack().index.is_monotonic
True

>>> data.sort_index().index.is_monotonic
True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM