pandas.read_pickle() method in Pandas

pandas.read_pickle() method from Pandas API (Application Programming Interface) is used to read pickled objects or other objects from files. This method is valuable when Python objects are read from files that are stored as streams of bits/bytes.

Syntax

It has the following syntax:

pandas.read_pickle(filepath_or_buffer, compression, storage_options)

Parameters

pandas.read_pickle() takes different argument values as below.

  • filepath_or_buffer: it could be String, Path-like object, or Path object. But updated in the latest versions of Pandas API where it also takes URLs. 
  • compression: the file which is going to be read can have compression. So, compression argument checks for renowned compression algorithms like ‘gzip', 'zip', 'bz2', 'tar', 'zstd'. It could be a Python dictionary or String. The default value is 'infer'.
  • storage_options (optional): Python dictionary-type object that specifies any additional storage connection like HTTP (Hypertext Transfer Protocol), port, username, or password. The default value is None.

Return Value

unpickled: It returns the object of the same type as it was saved in a file using the to_pickle() method.

Explanation

In this example, weโ€™re going to explain how pandas.read_pickle() works. Letโ€™s learn.

# importing packages
import pandas as pd

# dictionary of data
dct = {'Employee ID': {0: 23, 1: 43, 2: 12,
			3: 13, 4: 67, 5: 89},
	'Name': {0: 'Darryl I. Deen', 1: 'Anthony D. King',
				2: 'Wallace C. Dumais', 3: 'Charles C. Page',
				4: 'Graham J. Grasso', 5: 'Glenn C. Gonzalez'},
	'Department': {0: 'Computer Science', 1: 'Operations', 2: 'Marketing',
                3: 'Business Development', 4: 'Data Engineering', 5: 'Accounts'},
	'Designation': {0: 'Manager', 1: 'Team Member', 2: 'Technical Writer', 
                3: 'Team Member', 4: 'Team Lead',5:'Chief Manager'}
	}

# creating Data Frame by converting dict
df = pd.DataFrame(dct)

# invoking to_pickle() method 
# to write Data Frame into file "algoideas.xyz"
pd.to_pickle(df, './algoideas.xyz')

# invoking pd.read_pickle method
# unpickled the Data Frame from file "algoideas.xyz"
unpickled_data = pd.read_pickle("./algoideas.xyz")
print(unpickled_data)
  • Line#2: Importing Pandas module in python program.
  • Line#5-14: Created a Python dictionary that has data on employees including their Employee ID, Names, Department, and Designation.
  • Line#17: Creating Data Frame by converting Python dictionary into Data Frame
  • Line#21: Invoking to_pickle() method to writing Data Frame into a file "algoideas.xyz".
  • Line#25: Invoking pd.read_pickle("./algoideas.xyz") method unpickled the Data Frame from the file "algoideas.xyz".

Output: pandas.read_pickle()

read pickle in pandas
pandas.read_pickle() method output

Stay in the Loop

Get the daily email from Algoideas that makes reading the news actually enjoyable. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

- Advertisement -

You might also like...