Skip to content

BUG: DataFrame.shift(axis=1) with multiple blocks of same type #10539

Closed
@bwillers

Description

@bwillers

Shows up whenver you have multiple blocks of the same type in a dataframe.

Int example:

In [1]: a = pd.Series([1,2,3])   
In [2]: b = pd.Series([4,5,6])
In [3]: df1 = pd.DataFrame({'a': a, 'b': b})
In [4]: df2 = pd.DataFrame({'a': a}).assign(b=b)

In [5]: df1._data.blocks
Out[5]: (IntBlock: slice(0, 2, 1), 2 x 3, dtype: int64,)

In [6]: df2._data.blocks
Out[6]:
(IntBlock: slice(0, 1, 1), 1 x 3, dtype: int64,
 IntBlock: slice(1, 2, 1), 1 x 3, dtype: int64)

In [7]: df1
Out[7]:
   a  b
0  1  4
1  2  5
2  3  6

In [8]: df2
Out[8]:
   a  b
0  1  4
1  2  5
2  3  6

In [9]: df1.shift(1, axis=1)
Out[9]:
    a  b
0 NaN  1
1 NaN  2
2 NaN  3

In [10]: df2.shift(1, axis=1)
Out[10]:
    a   b
0 NaN NaN
1 NaN NaN
2 NaN NaN

Or with different data types:

In [2]: df = pd.DataFrame({'a': [1,2,3], 'b': ['A', 'B', 'C']})

In [3]: df
Out[3]:
   a  b
0  1  A
1  2  B
2  3  C

In [4]: df.shift(1, axis=1)
Out[4]:
    a    b
0 NaN  NaN
1 NaN  NaN
2 NaN  NaN

I expect a similar issue pops up with panels and ndframes when shifting across the axis where dtypes can change.

Perhaps this can be fixed by having shift move the column labels rather than the data here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDtype ConversionsUnexpected or buggy dtype conversionsInternalsRelated to non-user accessible pandas implementationMulti-BlockIssues caused by the presence of multiple BlocksNumeric OperationsArithmetic, Comparison, and Logical operationsReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions