Open In App

Python Pandas – pandas.api.types.is_file_like() Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

In this article, we will be looking toward the functionality of pandas.api.types.is_file_like() from the pandas.api.types module with its various examples in the Python language.

An object must be an iterator AND have a read or write method as an attribute to be called file-like. It is important to note that file-like objects must be iterable, but iterable objects do not have to be file-like.

Pandas.api.types.is_file_like() method is used to check if the object is a file-like object.

Syntax: pandas.api.types.is_file_like(obj)

Parameters:  obj: the object we pass in to check.

Returns:  boolean value. True if object has file like properties, false if it doesn’t.

Example 1:

Under this article, the pandas.api.types package and the NumPy package is imported and a NumPy array is checked if it’s a file-like object. As array object is not file like pandas.api.types.is_file_like() returns False .

Python3




# import packages
from pandas.api.types import is_file_like
import numpy as np
  
# checking if it's a file like object
print(is_file_like(np.array([4, 8, 2, 7])))


Output:

False

Example 2:

In this example, the StringIO is used which is an in-memory file-like object that performs newline changes. Since it is a file type object pandas.api.types.is_file_like() method returns True when checked.

Python3




# import packages
import io
from pandas.api.types import is_file_like
  
buffer = io.StringIO("geeksforgeeks")
# checking if it's a file like object
print(is_file_like(buffer))


Output:

True

Example 3:

under this example, a text file named “file1.txt” is checked if it’s a file-like object through the pandas.api.types.is_file_like() method returns True when the file object is checked if its file object type.

Click here to view the used file-file1.txt. 

Python3




# import packages
import pandas as pd
  
# opening a file
data = open('file1.txt')
  
# checking if it's a file like object
print(pd.api.types.is_file_like(data))


Output:

True


Last Updated : 28 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads