1

In the following code using python/pandas:

import numpy as np
import pandas as pd

s = pd.Series([1, 3, 5, 12, 6, 8])

print(s)

We get the following output:

0     1
1     3
2     5
3    12
4     6
5     8
dtype: int64

Why does it print out this line: dtype: int64 when I don't see that print statement anywhere in the code?

bluethundr
  • 1,005
  • 17
  • 68
  • 141
  • 2
    Probably looking for something along the lines of: https://stackoverflow.com/questions/29645153/remove-name-dtype-from-pandas-output, or if you just don't want the dtype: `print(s.to_string(dtype=None))` – ALollz Jul 30 '20 at 18:45
  • you mean to say the `dtype` shouldnt be printed? how do you know them if they dont. And what is he difficulty you face because of this..? would be interested to know – anky Jul 30 '20 at 19:11
  • I'm interested if someone knows more about the internals of exactly print(s). both `s.__repr__()` and `s.__str__()` default include the `dtype`. [`__repr__`](https://github.com/pandas-dev/pandas/blob/v1.1.0/pandas/core/series.py#L1297) clearly indictates it will include the dtype in line 1318 but I can't quite trace the bound __str__ method – ALollz Jul 30 '20 at 19:15
  • Printing a pandas series is usually intended for debugging, so it's helpful to include this detail. It's not designed as a user interface for end users. – Barmar Jul 30 '20 at 19:24
  • I was following a pandas lesson and just wondering why the `dtype: int64` line prints out when there is no print statement specifically that does that in the code. This is not mission critical work, I'm just following some examples to learn pandas and wanted to know why that extra line is there. – bluethundr Jul 30 '20 at 20:05

2 Answers2

0

I have an idea about the reason why it prints dtype: int64.

If you look at the current sobject, the type of s is Series.

I converted to the numpy array:

s = s.to_numpy(dtype=int)
print(s)

The result:

[ 1  3  5 12  6  8]
Ahmet
  • 7,527
  • 3
  • 23
  • 47
0

Hope this will help!

>>> import numpy as np
>>> import pandas as pd
>>>
>>> s = pd.Series([1, 3, 5, 12, 6, 8])
>>>
>>> print(s.to_string())
0     1
1     3
2     5
3    12
4     6
5     8
>>> print(s.values)
[ 1  3  5 12  6  8]
>>> print(pd.DataFrame(s))
    0
0   1
1   3
2   5
3  12
4   6
5   8

here you can refer to the documentation.

Ransaka Ravihara
  • 1,786
  • 1
  • 13
  • 30