# how to check all values in particular column has same data type or not?

I have column 'ABC' which has 5000 rows. Currently, dtype of column is object. Mostly it has string values but some values dtype is not string, I want to find all those rows and modify those rows. Column is as following:

1 abc
2 def
3 ghi
4 23
5 mno
6 null
7 qwe
8 12-11-2019
...
...
...
4900 ert
5000 tyu


In above case, I can use for loop to find out rows which do not have desired dtype. I just wanted to know, is their better way to solve this issue.

Note: I am using Pandas.

• In pandas dtypes can be inferred by trying to cast them and making un-castable ones to string dtypes as in object, which means all elements in a single column will be in a same datatype. You cant have two diff. row elements in the same column to be of different datatypes. – Kiritee Gak Sep 28 at 15:45
• @KiriteeGak: I think that is not quite true. You can test that yourself. Create a dataframe, with at least two rows indexed 1 and 2. Then do df.loc[1, 'new_column']= 'my_value'. Then do df['new_column'].map(type). You will see, that all but the first row contain floats. That is because the other rows contain NaN, which is a float and not a str. Likewise you could mix in other object types in your object column if you like (but it is probably not a very good idea). – jottbe Sep 28 at 15:54
• I stand corrected. Thanks :) – Kiritee Gak Sep 28 at 16:39

You can get the type of the entries of your column with map:

df['ABC'].map(type)


So to filter on all values, which are not stored as str, you can use:

df['ABC'].map(type) != str


If however you just want to check if some of the rows contain a string, that has a special format (like a date), you can check this with a regex like:

df['ABC'].str.match('[0-9]{4}-[0-9]{2}-[0-9]{2}')


But of course, that is no exact date check. E.g. it would also return True for values like 0000-13-91, but this was only meant to give you an idea anyways.

• thanks, it helped.. – Kiran Sep 28 at 15:59