Skip to content

is it better for $select method with integer index to abort or return the columns? #990

Closed as not planned

Description

@Yunuuuu

Usually, this is not what user wanted

polars::pl$DataFrame(mtcars)$select(1:2)
#> shape: (2, 1)
#> ┌─────┐
#> │     │
#> │ --- │
#> │ i32 │
#> ╞═════╡
#> │ 1   │
#> │ 2   │
#> └─────┘
data.table::as.data.table(mtcars)[, 1:2]
#>       mpg   cyl
#>     <num> <num>
#>  1:  21.0     6
#>  2:  21.0     6
#>  3:  22.8     4
#>  4:  21.4     6
#>  5:  18.7     8
#>  6:  18.1     6
#>  7:  14.3     8
#>  8:  24.4     4
#>  9:  22.8     4
#> 10:  19.2     6
#> 11:  17.8     6
#> 12:  16.4     8
#> 13:  17.3     8
#> 14:  15.2     8
#> 15:  10.4     8
#> 16:  10.4     8
#> 17:  14.7     8
#> 18:  32.4     4
#> 19:  30.4     4
#> 20:  33.9     4
#> 21:  21.5     4
#> 22:  15.5     8
#> 23:  15.2     8
#> 24:  13.3     8
#> 25:  19.2     8
#> 26:  27.3     4
#> 27:  26.0     4
#> 28:  30.4     4
#> 29:  15.8     8
#> 30:  19.7     6
#> 31:  15.0     8
#> 32:  21.4     4
#>       mpg   cyl
dplyr::select(mtcars, 1:2)
#>                      mpg cyl
#> Mazda RX4           21.0   6
#> Mazda RX4 Wag       21.0   6
#> Datsun 710          22.8   4
#> Hornet 4 Drive      21.4   6
#> Hornet Sportabout   18.7   8
#> Valiant             18.1   6
#> Duster 360          14.3   8
#> Merc 240D           24.4   4
#> Merc 230            22.8   4
#> Merc 280            19.2   6
#> Merc 280C           17.8   6
#> Merc 450SE          16.4   8
#> Merc 450SL          17.3   8
#> Merc 450SLC         15.2   8
#> Cadillac Fleetwood  10.4   8
#> Lincoln Continental 10.4   8
#> Chrysler Imperial   14.7   8
#> Fiat 128            32.4   4
#> Honda Civic         30.4   4
#> Toyota Corolla      33.9   4
#> Toyota Corona       21.5   4
#> Dodge Challenger    15.5   8
#> AMC Javelin         15.2   8
#> Camaro Z28          13.3   8
#> Pontiac Firebird    19.2   8
#> Fiat X1-9           27.3   4
#> Porsche 914-2       26.0   4
#> Lotus Europa        30.4   4
#> Ford Pantera L      15.8   8
#> Ferrari Dino        19.7   6
#> Maserati Bora       15.0   8
#> Volvo 142E          21.4   4

Created on 2024-03-30 with reprex v2.0.2

Activity

  1. etiennebacher commented on Mar 30, 2024

    @etiennebacher
    Collaborator

    This is the way it works in python polars and we try to stick to it:

    >>> pl.DataFrame({"x": [1, 2, 3]}).select(1)
    shape: (1, 1)
    ┌─────────┐
    │ literal │
    │ ---     │
    │ i32     │
    ╞═════════╡
    │ 1       │
    └─────────┘

    However, you can select by position using [:

    > pl$DataFrame(mtcars)[, 2:4]
    shape: (32, 3)
    ┌─────┬───────┬───────┐
    │ cyl ┆ disp  ┆ hp    │
    │ --- ┆ ---   ┆ ---   │
    │ f64 ┆ f64   ┆ f64   │
    ╞═════╪═══════╪═══════╡
    │ 6.0 ┆ 160.0 ┆ 110.0 │
    │ 6.0 ┆ 160.0 ┆ 110.0 │
    │ 4.0 ┆ 108.0 ┆ 93.0  │
    │ 6.0 ┆ 258.0 ┆ 110.0 │
    │ 8.0 ┆ 360.0 ┆ 175.0 │
    │ …   ┆ …     ┆ …     │
    │ 4.0 ┆ 95.1  ┆ 113.0 │
    │ 8.0 ┆ 351.0 ┆ 264.0 │
    │ 6.0 ┆ 145.0 ┆ 175.0 │
    │ 8.0 ┆ 301.0 ┆ 335.0 │
    │ 4.0 ┆ 121.0 ┆ 109.0 │
    └─────┴───────┴───────┘
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions