w3resource

Pandas Series: to_pickle() function

Series-to_pickle() function

Pickle (serialize) object to file.

Syntax:

Series.to_pickle(self, path, compression='infer', protocol=4)
Pandas Series: str.to_pickle() function

Parameters:

Name Description Type/Default Value Required / Optional
path File path where the pickled object will be stored str Required
compression A string representing the compression to use in the output file. By default, infers from the file extension in specified path. {‘infer’, ‘gzip’, ‘bz2’, ‘zip’, ‘xz’, None}, default ‘infer’ Required
protocol Int which indicates which protocol should be used by the pickler, default HIGHEST_PROTOCOL. The possible values are 0, 1, 2, 3, 4. A negative value for the protocol parameter is equivalent to setting its value to HIGHEST_PROTOCOL. int Required

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
original_df = pd.DataFrame({"f1": range(6), "b1": range(6, 12)})
original_df

Output:

  f1	b1
0	0	6
1	1	7
2	2	8
3	3	9
4	4	10
5	5	11
Pandas Series: str.to_pickle() function

Python-Pandas Code:

import numpy as np
import pandas as pd
original_df = pd.DataFrame({"f1": range(6), "b1": range(6, 12)})
original_df.to_pickle("./dummy.pkl")
unpickled_df = pd.read_pickle("./dummy.pkl")
unpickled_df

Output:

  f1	b1
0	0	6
1	1	7
2	2	8
3	3	9
4	4	10
5	5	11

Python-Pandas Code:

import numpy as np
import pandas as pd
original_df.to_pickle("./dummy.pkl")
unpickled_df = pd.read_pickle("./dummy.pkl")
import os
os.remove("./dummy.pkl")

Previous: Series-plot.pie() function
Next: Series-to_csv() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/pandas/series/series-to_pickle.php