Python FutureWarning: The default dtype for empty Series will be 'object'

Viewed 995

When I try to create an empty series:

my_series = pd.Series()

I am prompted to use an explicit datatype:

FutureWarning: The default dtype for empty Series will be 'object'
instead of 'float64' in a future version. Specify a dtype explicitly
to silence this warning.

In the series, I will store pandas timestamps, which I don't know what's the string to use for the dtype. I can't find it. How can I remove those annoying warnings?

1 Answers

You're looking for datetime64[ns].

my_series = pd.Series(dtype='datetime64[ns]')

Proof:

>>> date_series = pd.Series([pd.Timestamp('today')])
>>> str(date_series.dtype)
'datetime64[ns]'

For completeness: You could also use <M8[ns], but that's much less understandable (in my opinion), and less portable, therefore I highly recommend datetime64[ns] instead.

Related