Skip to content
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

ENH: Add unit keyword to Timestamp and to_datetime #3818

Merged
merged 3 commits into from
Jun 13, 2013

Conversation

jreback
Copy link
Contributor

@jreback jreback commented Jun 9, 2013

to enable passing of integers or floats that are in an epoch unit of s, ms, us, ns
(e.g. unix timestamps or epoch s, with fracional seconds allowed)

closes #3540

In [5]: pd.to_datetime(Series([ 1370745748 + t for t in range(5) ]),unit='s')
Out[5]: 
0   2013-06-09 02:42:28
1   2013-06-09 02:42:29
2   2013-06-09 02:42:30
3   2013-06-09 02:42:31
4   2013-06-09 02:42:32
dtype: datetime64[ns]

@hayd
Copy link
Contributor

hayd commented Jun 9, 2013

(I wonder if it makes it less ambiguous here to have the default unit for to_datetime as 'ns' rather than None?)

@jreback
Copy link
Contributor Author

jreback commented Jun 9, 2013

they are both treated the same (but i'll change it in any event)

@jreback
Copy link
Contributor Author

jreback commented Jun 9, 2013

actually...going to leav e it as None, becuase it doens' tapply in most cases and might be confusing?

@hayd
Copy link
Contributor

hayd commented Jun 9, 2013

I was thinking that too, but I think a little rewording may solve it, I was going to suggest something like:

unit : unit of the arg (s,ms,us,ns) if passed an epoch time # numerical?
    (e.g. a unix timestamp)

@jreback
Copy link
Contributor Author

jreback commented Jun 9, 2013

ok...that's reasonable....sure

@jreback
Copy link
Contributor Author

jreback commented Jun 9, 2013

@wesm any thoughts?

@hayd
Copy link
Contributor

hayd commented Jun 12, 2013

This used to cause an exception, but maybe now is a good time for this to return NaT? :)

In your branch:

In [2]: pd.Timestamp(np.nan)
Exception ValueError: 'cannot convert float NaN to integer' in 'pandas.tslib.cast_from_unit' ignored
Out[2]: Timestamp('1970-01-01 00:00:00', tz=None)

@jreback
Copy link
Contributor Author

jreback commented Jun 12, 2013

think I got all of the cases, thanks!

In [2]: Timestamp(np.nan)
Out[2]: NaT

In [3]: Timestamp(pd.NaT)
Out[3]: NaT

In [5]: Timestamp(pd.tslib.iNaT)
Out[5]: NaT

In [6]: Timestamp(float(pd.tslib.iNaT))
Out[6]: NaT

@jreback
Copy link
Contributor Author

jreback commented Jun 12, 2013

@wesm ok API by you?

    integers or floats that are in an epoch unit of s, ms, us, ns
    (e.g. unix timestamps or epoch s, with fracional seconds allowed)
    (GH 3540)
TST: manage truediv in py3 for unit comparisons
jreback added a commit that referenced this pull request Jun 13, 2013
ENH: Add unit keyword to Timestamp and to_datetime
@jreback jreback merged commit cf0db6f into pandas-dev:master Jun 13, 2013
@@ -69,6 +69,8 @@ def to_datetime(arg, errors='ignore', dayfirst=False, utc=None, box=True,
format : string, default None
strftime to parse time, eg "%d/%m/%Y"
coerce : force errors to NaT (False by default)
unit : unit of the arg (s,ms,us,ns) denote the unit in epoch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think maybe this should say "unit of the arg (s,ms,us,ns) if passed an epoch", will do that in that pr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go ahed....just merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Timestamp should have alternate constructor for UTC millisecond timestamps
2 participants