0

I do not know whether to call it a small matter but it has been puzzling me. numpy.dtype prints differently formatted outputs, one without print statement and one with print statement. Here is an example:

train = pd.read_csv("train.csv")
In [10]: train.dtypes
Out[10]:
first_active_month     object
card_id                object
feature_1               int64
feature_2               int64
feature_3               int64
target                float64
dtype: object

In [5]: train['card_id'].dtypes
Out[5]: dtype('O')

In [11]: print(train['card_id'].dtypes)
object

Kindly see the output of last two commands. When printed without using the 'print' statement, output is 'dtype('O')' and when printed with 'print' statement, the output is 'object'.

I have read numpy.dtype manual but could not find the reasons for these two different output formats. Also, the following works:

In [16]: train['card_id'].dtypes in ['object']
Out[16]: True

I will be grateful to know the reasons.

2 Answers 2

2

When you evaluate an expression in the REPL, it prints the repr of an object. When you use the print function, it tries the str of the object first, i.e. the methods corresponding to __repr__ and __str__ for the class that defined the type.

So try

print(repr(train['card_id'].dtypes))
print(str(train['card_id'].dtypes))
Sign up to request clarification or add additional context in comments.

Comments

1

They are the same thing and have equivalency. It's just that print() parses uniquely.

dtype('O') = a python object.

Dtypes

1 Comment

I think you are right. When I write ' train['card_id'].dtypes.name' on the command line, output is 'object' . It means 'print' statement is printing the 'name' attribute of dytpe object. Is this behavior of print statement always true that is to just print out the name attribute of the object, if it exists?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.