Skip to content

Cannot convert a datetimelike to 'str' with astype #9757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wavexx opened this issue Mar 31, 2015 · 8 comments
Closed

Cannot convert a datetimelike to 'str' with astype #9757

wavexx opened this issue Mar 31, 2015 · 8 comments
Labels
API Design Datetime Datetime data dtype Timedelta Timedelta data type
Milestone

Comments

@wavexx
Copy link

wavexx commented Mar 31, 2015

I find this surprising:

import pandas as pd
pd.Series([pd.Timestamp('2010-01-04 00:00:00')]).astype(str)
TypeError: cannot astype a datetimelike from [datetime64[ns]] to [|S0]

str(pd.Timestamp) works though, so why astype shouldn't?
Also the type representation "|S0" is not entirely clear to me.

@jreback
Copy link
Contributor

jreback commented Mar 31, 2015

this is string formatting and not what astype does which is convert to a fixed width string dtype which is not the same as a str representation

that's why this is not allowed
further there are many possible formats for strings
use .format()

it's possible that astype should just do the default formatting but I'd like to see some justification for this which addresses the points above

@evanpw
Copy link
Contributor

evanpw commented Mar 31, 2015

This conversion is explicitly allowed for every other type (e.g., conversion to string is checked for directly in core.common._astype_nansafe).

In numpy, a call to astype(str) converts to a fixed-width string with a size large enough to contain every entry:

In [2]: np.array([np.datetime64('2010-01-03 00:00:00')]).astype(str)
Out[2]:
array(['2010-01-03T05:00:00Z'],
      dtype='|S38')

That might be a better choice, but it's not what's currently being done for types other than datetime64 and timedelta64:

In [3]: pd.Series([1.5]).astype(str)
Out[3]:
0    1.5
dtype: object

I actually can't get astype to convert to a fixed-width string even if I try this:

In [2]: pd.Series([1.5]).astype('S10')
Out[2]:
0    1.5
dtype: object

@wavexx
Copy link
Author

wavexx commented Mar 31, 2015

I personally expect conversion to 'str' to always work and default to __str__.
I know format it's an option, but it shouldn't be mandatory.

I was trying to convert an entire DataFrame to 'str' (for quick displaying purposes, nothing fancy).
It's worth nothing that:

pd.Series([pd.Timestamp('2010-01-04 00:00:00')]).astype(object).astype(str)

does what I had in mind but adds an useless conversion.

@evanpw
Copy link
Contributor

evanpw commented Mar 31, 2015

Actually, it looks like I'm confused about fixed-width string dtypes. Is it even possible to store such a thing in a Series or DataFrame? I can't seem to find any way.

@jreback
Copy link
Contributor

jreback commented Mar 31, 2015

fixed width string dtypes are not supported and converted to object

@shoyer
Copy link
Member

shoyer commented Mar 31, 2015

It's true that we don't support fixed width string dtypes, but it would be nice for .astype(str) to reliably coerce to an object array of strings.

@jorisvandenbossche
Copy link
Member

I also see no reason to not let astype(str) convert it to an object array of strings with default formatting.

Apart from that, we could maybe also use a separate formatting function (similar to strftime for datetime.datetime)

@jreback jreback added Datetime Datetime data dtype API Design Timedelta Timedelta data type labels Apr 2, 2015
@jreback jreback added this to the 0.16.1 milestone Apr 8, 2015
@jreback
Copy link
Contributor

jreback commented Apr 11, 2015

closed by #9758

@jreback jreback closed this as completed Apr 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Datetime Datetime data dtype Timedelta Timedelta data type
Projects
None yet
Development

No branches or pull requests

5 participants