Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Polars Cookbook

You're reading from   Polars Cookbook Over 60 practical recipes to transform, manipulate, and analyze your data using Python Polars 1.x

Arrow left icon
Product type Paperback
Published in Aug 2024
Publisher Packt
ISBN-13 9781805121152
Length 394 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Yuki Kakegawa Yuki Kakegawa
Author Profile Icon Yuki Kakegawa
Yuki Kakegawa
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Chapter 1: Getting Started with Python Polars FREE CHAPTER 2. Chapter 2: Reading and Writing Files 3. Chapter 3: An Introduction to Data Analysis in Python Polars 4. Chapter 4: Data Transformation Techniques 5. Chapter 5: Handling Missing Data 6. Chapter 6: Performing String Manipulations 7. Chapter 7: Working with Nested Data Structures 8. Chapter 8: Reshaping and Tidying Data 9. Chapter 9: Time Series Analysis 10. Chapter 10: Interoperability with Other Python Libraries 11. Chapter 11: Working with Common Cloud Data Sources 12. Chapter 12: Testing and Debugging in Polars 13. Index 14. Other Books You May Enjoy

Polars Series

Series is an important concept in a DataFrame library. A DataFrame is made up of one or more Series. A Series is like a list or array: it’s a one-dimensional structure that stores a list of values. A Series is different than a list or array in Python in that a Series is viewed as a column in a table, containing the list of data points or values of a certain data type. Just like the Polars DataFrame, the Polars Series also has many built-in methods you can utilize for your data transformations. In this recipe, we’ll cover the creation of Polars Series as well as how to inspect its attributes.

Getting ready

As usual, make that sure you import the Polars library at the beginning of your code if you haven’t already:

import polars as pl

How to do it...

We’ll first create a Series and explore its attributes.

  1. Create a Series from scratch:
    s = pl.Series('col', [1,2,3,4,5])
    s.head()

    The preceding code will return the following output:

Figure 1.6 – Polars Series

Figure 1.6 – Polars Series

  1. Create a Series from a DataFrame with the .to_series() and .get_column() methods:
    1. First, let’s convert a DataFrame to a Series with .to_series():
      data = {'a': [1,2,3], 'b': [4,5,6]}
      s_a = (
          pl.DataFrame(data)
          .to_series()
      )
      s_a.head()

    The preceding code will return the following output:

Figure 1.7 – A Series from a DataFrame

Figure 1.7 – A Series from a DataFrame

  1. By default, .to_series() returns the first column. You can specify the column by either index:
    s_b = (
        pl.DataFrame(data)
        .to_series(1)
    )
    s_b.head()
  2. When you want to retrieve a column for a Series, you can use .get_columns() instead:
    s_b2 = (
        pl.DataFrame(data)
        .get_column('b')
    )
    s_b2.head()

The preceding code will return the following output:

Figure 1.8 – Different ways to extract a Series from a DataFrame

Figure 1.8 – Different ways to extract a Series from a DataFrame

  1. Display Series attributes:
    1. Get the length and width with .shape:
      s.shape

    The preceding code will return the following output:

    >> (5,)
    1. Use .name to get the column name:
      s.name

    The preceding code will return the following output:

    >> 'col'
    1. .dtype gives you the data type:
      s.dtype

    The preceding code will return the following output:

    >> Int64

How it works...

The process of creating a Series and getting its attributes is similar to that of creating a DataFrame. There are many other methods that are common across DataFrame and Series. Knowing how to work with DataFrame means knowing how to work with Series and vice-versa.

There’s more...

Just like DataFrame, Series can be converted between other structures such as a NumPy array and pandas Series. We won’t get into details on that in this book, but we’ll go over this for DataFrame later in the book in Chapter 10, Interoperability with Other Python Libraries.

See also

If you’d like to learn more, please visit Polars’ documentation page: https://pola-rs.github.io/polars/py-polars/html/reference/series/index.html.

You have been reading a chapter from
Polars Cookbook
Published in: Aug 2024
Publisher: Packt
ISBN-13: 9781805121152
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime