OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

pandas df.dtypes does not identify timestamps data type correctly...? [duplicate]

  • Thread starter Thread starter Fabian Werner
  • Start date Start date
F

Fabian Werner

Guest
I have the following pandas dataframe at hand:

Code:
          key                    created
0  DLAND-1957  2024-05-23 12:59:25+02:00
1  DLAND-1956  2024-05-22 13:53:09+01:00

it is being created in this way:

Code:
import pandas as pd
key = ["DLAND-1957", "DLAND-1956"]
created = ["2024-05-23 12:59:25+02:00", "2024-05-22 13:53:09+01:00"]
df = pd.DataFrame({"key":key, "created":created})
df["created"] = pd.to_datetime(df["created"])

As you can see, the column "created" is a "timestamp":

Code:
type(list(df.iloc[0])[1])
type(list(df.iloc[1])[1])

both return

Code:
pandas._libs.tslibs.timestamps.Timestamp

However,

Code:
df.dtypes

returns

Code:
key        object
created    object

So just the mere fact that timestamps are present from different timezones makes pandas generalize the type of the column to become "object"? That is a bit problematic when trying to detect data types in a pandas dataframe... how (if not using df.dtypes) do I correctly detect timestamp and other data types in a pandas dataframe?
<p>I have the following pandas dataframe at hand:</p>
<pre><code> key created
0 DLAND-1957 2024-05-23 12:59:25+02:00
1 DLAND-1956 2024-05-22 13:53:09+01:00
</code></pre>
<p>it is being created in this way:</p>
<pre><code>import pandas as pd
key = ["DLAND-1957", "DLAND-1956"]
created = ["2024-05-23 12:59:25+02:00", "2024-05-22 13:53:09+01:00"]
df = pd.DataFrame({"key":key, "created":created})
df["created"] = pd.to_datetime(df["created"])
</code></pre>
<p>As you can see, the column "created" is a "timestamp":</p>
<pre><code>type(list(df.iloc[0])[1])
type(list(df.iloc[1])[1])
</code></pre>
<p>both return</p>
<pre><code>pandas._libs.tslibs.timestamps.Timestamp
</code></pre>
<p>However,</p>
<pre><code>df.dtypes
</code></pre>
<p>returns</p>
<pre><code>key object
created object
</code></pre>
<p>So just the mere fact that timestamps are present from different timezones makes pandas generalize the type of the column to become "object"? That is a bit problematic when trying to detect data types in a pandas dataframe... how (if not using df.dtypes) do I correctly detect timestamp and other data types in a pandas dataframe?</p>
 

Latest posts

S
Replies
0
Views
1
Safwan Aipuram
S
Top