ENH: provide "inplace" argument to set_axis(), change signature · rs2/pandas@409f502 (original) (raw)

`@@ -466,9 +466,91 @@ def _expand_axes(self, key):

`

466

466

``

467

467

`return new_axes

`

468

468

``

469

``

`-

def set_axis(self, axis, labels):

`

470

``

`-

""" public verson of axis assignment """

`

471

``

`-

setattr(self, self._get_axis_name(axis), labels)

`

``

469

`+

_shared_docs['set_axis'] = """Assign desired index to given axis

`

``

470

+

``

471

`+

Parameters

`

``

472

`+


`

``

473

`+

labels: list-like or Index

`

``

474

`+

The values for the new index

`

``

475

`+

axis : int or string, default 0

`

``

476

`+

inplace : boolean, default None

`

``

477

`+

Whether to return a new %(klass)s instance.

`

``

478

+

``

479

`+

WARNING: inplace=None currently falls back to to True, but

`

``

480

`+

in a future version, will default to False. Use inplace=True

`

``

481

`+

explicitly rather than relying on the default.

`

``

482

+

``

483

`+

.. versionadded:: 0.21.0

`

``

484

`+

The signature was uniformed to the rest of the API: previously,

`

``

485

`+

"axis" and "labels" were respectively the first and second

`

``

486

`+

positional arguments.

`

``

487

+

``

488

`+

Returns

`

``

489

`+


`

``

490

`+

renamed : %(klass)s or None

`

``

491

`+

New object if inplace=False, None otherwise.

`

``

492

+

``

493

`+

See Also

`

``

494

`+


`

``

495

`+

pandas.NDFrame.rename

`

``

496

+

``

497

`+

Examples

`

``

498

`+


`

``

499

`+

s = pd.Series([1, 2, 3])

`

``

500

`+

s

`

``

501

`+

0 1

`

``

502

`+

1 2

`

``

503

`+

2 3

`

``

504

`+

dtype: int64

`

``

505

`+

s.set_axis(0, ['a', 'b', 'c'], inplace=False)

`

``

506

`+

a 1

`

``

507

`+

b 2

`

``

508

`+

c 3

`

``

509

`+

dtype: int64

`

``

510

`+

df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})

`

``

511

`+

df.set_axis(0, ['a', 'b', 'c'], inplace=False)

`

``

512

`+

A B

`

``

513

`+

a 1 4

`

``

514

`+

b 2 5

`

``

515

`+

c 3 6

`

``

516

`+

df.set_axis(1, ['I', 'II'], inplace=False)

`

``

517

`+

I II

`

``

518

`+

0 1 4

`

``

519

`+

1 2 5

`

``

520

`+

2 3 6

`

``

521

`+

df.set_axis(1, ['i', 'ii'], inplace=True)

`

``

522

`+

df

`

``

523

`+

i ii

`

``

524

`+

0 1 4

`

``

525

`+

1 2 5

`

``

526

`+

2 3 6

`

``

527

+

``

528

`+

"""

`

``

529

+

``

530

`+

@Appender(_shared_docs['set_axis'] % dict(klass='NDFrame'))

`

``

531

`+

def set_axis(self, labels, axis=0, inplace=None):

`

``

532

`+

if is_scalar(labels):

`

``

533

`+

warnings.warn(

`

``

534

`+

"set_axis now takes "labels" as first argument, and "

`

``

535

`+

""axis" as named parameter. The old form, with "axis" as "

`

``

536

`+

"first parameter and "labels" as second, is still supported "

`

``

537

`+

"but will be deprecated in a future version of pandas.",

`

``

538

`+

FutureWarning, stacklevel=2)

`

``

539

`+

labels, axis = axis, labels

`

``

540

+

``

541

`+

if inplace is None:

`

``

542

`+

warnings.warn(

`

``

543

`+

"set_axis currently defaults to operating inplace.\nThis "

`

``

544

`+

"will change in a future version of pandas, use "

`

``

545

`+

"inplace=True to avoid this warning.",

`

``

546

`+

FutureWarning, stacklevel=2)

`

``

547

`+

inplace = True

`

``

548

`+

if inplace:

`

``

549

`+

setattr(self, self._get_axis_name(axis), labels)

`

``

550

`+

else:

`

``

551

`+

obj = self.copy()

`

``

552

`+

obj.set_axis(labels, axis=axis, inplace=True)

`

``

553

`+

return obj

`

472

554

``

473

555

`def _set_axis(self, axis, labels):

`

474

556

`self._data.set_axis(axis, labels)

`