0

I have a dataframe with stock info

stocks_df.head()

    Symbol  Security    GICS Sector GICS Sub Industry   Price_1_1   Price_3_23  Price_4_9   Shares
index                               
0   MMM 3M Company  Industrials Industrial Conglomerates    180 117.87  147.78  575261000
1   ABT Abbott Laboratories Health Care Health Care Equipment   86.95   62.82   86.04   1763433000
2   ABBV    AbbVie Inc. Health Care Pharmaceuticals 89.55   64.5    79.75   1476673000
3   ABMD    ABIOMED Inc Health Care Health Care Equipment   168.81  132.34  160.08  45063000
4   ACN Accenture plc   Information Technology  IT Consulting & Other Services  210.15  143.69  177.92  637027000

when I look at the datatypes I get

stocks_df.dtypes

Symbol               object
Security             object
GICS Sector          object
GICS Sub Industry    object
Price_1_1            object
Price_3_23           object
Price_4_9            object
Shares               object
dtype: object

I realize that pandas provides lightning fast access by using an array of pointers to string objects. This is clearly discussed in these threads.

Strings in a DataFrame, but dtype is object

https://www.gormanalysis.com/blog/python-pandas-for-your-grandpa-series-creation/

But when I check the datatype of a particular column, I find the type, series, not object

print(type(stocks_df["Shares"]))

<class 'pandas.core.series.Series'>

but the actual entries are all strings

print((stocks_df["Shares"][0]))
print(type(stocks_df["Shares"][0]))

575261000
<class 'str'>

OTOH, If I try to look up an entry this way

print(stocks_df.iloc[:1, stocks_df.columns.get_loc('Shares')],'\n')
print(type(stocks_df.iloc[:1, stocks_df.columns.get_loc('Shares')]))

I get

index
0    575261000
Name: Shares, dtype: object 

<class 'pandas.core.series.Series'>

This dataframe was created from a spreadsheet of string values.

In the first query, stocks_df.dtypes, each column is of type, object.

In the second query, type(stocks_df["Shares"]), a column is of type, series.

What is the appropriate syntax to get the actual datatype of each entry in a row?

I searched before asking this question and these threads

pandas how to check dtype for all columns in a dataframe?

https://www.pythonprogramming.in/how-to-check-the-data-type-of-dataframe-columns-in-pandas.html

say that my query, stocks_df.dtypes, should have shown "class 'str" for each entry.

aquagremlin
  • 3,515
  • 2
  • 29
  • 51
  • This answer (https://stackoverflow.com/a/44710678/13070032) seems to suggest : > since strings data types have variable length, it is by default stored as object dtype – Partha Mandal Apr 16 '20 at 23:04
  • 1
    We can use `stocks_df.applymap(type)` or for a Series(like a column or row in DataFrame) `s.apply(type)` – ansev Apr 16 '20 at 23:22
  • thank you ansev. make your comment an answer so i can mark it so. – aquagremlin Apr 17 '20 at 16:30

0 Answers0