markfairbanks / tidypolars

Tidy interface to polars
http://tidypolars.readthedocs.io
MIT License
337 stars 11 forks source link

Basics: tp.read_csv(), df.drop(x1, x2, x3, ...), and df.colnames? #198

Closed exsell-jc closed 2 years ago

exsell-jc commented 2 years ago

Really new to the library, but looking at the documentation did not really help with understanding.

Problem 1

import polars as pl
import tidypolars as tp
import csv
import requests

url = f'https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-05-24/Scrumqueens-data-2022-05-23.csv'

df = tp.read_csv(file = url) # does not work
df = pl.read_csv(file = url) # works??

Problem 2

df = df.drop('...1', 'Notes') # does not work
df = df.drop('...1') # works separately
df = df.drop('Notes') # works separately

Problem 3

df.colnames
df.names
df.colnames()
df.names()
# None of these work

What am I missing, exactly?

markfairbanks commented 2 years ago

tp.read_csv() works when I use it - what error do you get?

As far as the rest - if you are using pl.read_csv() it will return a polars DataFrame instead of a tidypolars Tibble. In that case you won't be able to use tidypolars methods on them. polars has a .drop() method that only allows one input which is why they were working separately in your example. You would need to convert the DataFrame to a Tibble using tp.from_polars(df) so you can use the tidypolars .drop() method.

However you should be able to use tp.read_csv() here, in which case you wouldn't need the tp.from_polars(df) step.

import tidypolars as tp

url = f'https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-05-24/Scrumqueens-data-2022-05-23.csv'

df = tp.read_csv(file = url)

df = df.drop('...1', 'Notes')

df_names = df.names

df.head()
┌─────────────┬─────────────┬─────────┬─────────┬─────┬────────┬────────┬─────────────┬────────────┐
│ Date        ┆ Team 1      ┆ Score 1 ┆ Score 2 ┆ ... ┆ Series ┆ Margin ┆ Winner      ┆ Loser      │
│ ---         ┆ ---         ┆ ---     ┆ ---     ┆     ┆ ---    ┆ ---    ┆ ---         ┆ ---        │
│ str         ┆ str         ┆ str     ┆ str     ┆     ┆ i64    ┆ str    ┆ str         ┆ str        │
╞═════════════╪═════════════╪═════════╪═════════╪═════╪════════╪════════╪═════════════╪════════════╡
│ 1997-03-15  ┆ New Zealand ┆ 54      ┆ 0       ┆ ... ┆ 1      ┆ 54     ┆ New Zealand ┆ Japan      │
│             ┆ Wild Ducks  ┆         ┆         ┆     ┆        ┆        ┆ Wild Ducks  ┆            │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 1997-03-15  ┆ England     ┆ 10      ┆ 7       ┆ ... ┆ 1      ┆ 3      ┆ England     ┆ Australia  │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 1997-03-15  ┆ Canada      ┆ 31      ┆ 0       ┆ ... ┆ 1      ┆ 31     ┆ Canada      ┆ Netherland │
│             ┆             ┆         ┆         ┆     ┆        ┆        ┆             ┆ s          │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 1997-03-15  ┆ Hong Kong   ┆ 12      ┆ 5       ┆ ... ┆ 1      ┆ 7      ┆ Hong Kong   ┆ Fiji       │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 1997-03-15  ┆ United      ┆ 26      ┆ 5       ┆ ... ┆ 1      ┆ 21     ┆ United      ┆ Scotland   │
│             ┆ States      ┆         ┆         ┆     ┆        ┆        ┆ States      ┆            │
└─────────────┴─────────────┴─────────┴─────────┴─────┴────────┴────────┴─────────────┴────────────┘
exsell-jc commented 2 years ago

It works like magic now, thanks