Skip to content

BUG: Timestamp constructor not handling timezone conversions correctly #2789

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
stephenwlin opened this issue Feb 1, 2013 · 1 comment
Closed
Labels
Milestone

Comments

@stephenwlin
Copy link
Contributor

Timestamp constructor raises an exception when given iso8601 string with UTC offset and a desired timezone. When given datetime object with timezone and a desired timezone, it gives a correct string but incorrect internal timestamp value.

In [1]: from pandas import Timestamp as ts, DataFrame as df
In [2]: from datetime import datetime as dt
In [3]: from pytz import timezone as tz
In [4]: UTC='UTC'
In [5]: EST='US/Eastern'
In [6]: CST='US/Central'
In [7]: # all the following should have the same timestamp value
In [8]: # (commented lines throw exceptions!!)
In [9]: tss=[ts("2012-01-02 01:00:00+00:00"),
   ...: #    ts("2012-01-02 01:00:00+00:00", tz=EST),
   ...:      ts("2012-01-01 20:00:00", tz=EST),
   ...:      ts(dt(2012,1,1,20,0,0,tzinfo=tz(EST))),
   ...:      ts(dt(2012,1,1,19,0,0,tzinfo=tz(CST))),
   ...:      ts(dt(2012,1,1,20,0,0,tzinfo=tz(EST)), tz=EST),
   ...:      ts(dt(2012,1,1,19,0,0,tzinfo=tz(CST)), tz=EST),
   ...:      ts("2012-01-01 20:00:00-05:00"),
   ...:      ts("2012-01-01 19:00:00-06:00"),
   ...: #    ts("2012-01-01 20:00:00-05:00", tz=EST),
   ...: #    ts("2012-01-01 19:00:00-06:00", tz=EST),
   ...:      ts(1325466000000000000, tz=UTC),
   ...:      ts(1325466000000000000, tz=EST)]

In [10]: strs = [str(ts) for ts in tss]
In [11]: vals = [(ts.value/1000000000000) for ts in tss]
In [12]: df({'strs' : strs, 'vals' : vals})
Out[12]: 
                        strs     vals
0  2012-01-02 01:00:00+00:00  1325466
1  2012-01-01 20:00:00-05:00  1325466
2  2012-01-01 20:00:00-05:00  1325466
3  2012-01-01 19:00:00-06:00  1325466
4  2012-01-01 20:00:00-05:00  1325448 <-- bad value!!
5  2012-01-01 20:00:00-05:00  1325448 <-- bad value!!
6  2012-01-01 20:00:00-05:00  1325466
7  2012-01-01 19:00:00-06:00  1325466
8  2012-01-02 01:00:00+00:00  1325466
9  2012-01-01 20:00:00-05:00  1325466
@stephenwlin
Copy link
Contributor Author

after fix:

In [1]: from pandas import Timestamp as ts, DataFrame as df
In [2]: from datetime import datetime as dt
In [3]: from pytz import timezone as tz
In [4]: UTC='UTC'
In [5]: EST='US/Eastern'
In [6]: CST='US/Central'
In [7]: # all the following should have the same timestamp value
In [8]: tss=[ts("2012-01-02 01:00:00+00:00"),
   ...:      ts("2012-01-02 01:00:00+00:00", tz=EST),
   ...:      ts("2012-01-01 20:00:00", tz=EST),
   ...:      ts(dt(2012,1,1,20,0,0,tzinfo=tz(EST))),
   ...:      ts(dt(2012,1,1,19,0,0,tzinfo=tz(CST))),
   ...:      ts(dt(2012,1,1,20,0,0,tzinfo=tz(EST)), tz=EST),
   ...:      ts(dt(2012,1,1,19,0,0,tzinfo=tz(CST)), tz=EST),
   ...:      ts("2012-01-01 20:00:00-05:00"),
   ...:      ts("2012-01-01 19:00:00-06:00"),
   ...:      ts("2012-01-01 20:00:00-05:00", tz=EST),
   ...:      ts("2012-01-01 19:00:00-06:00", tz=EST),
   ...:      ts(1325466000000000000, tz=UTC),
   ...:      ts(1325466000000000000, tz=EST)]

In [9]: strs = [str(ts) for ts in tss]
In [10]: vals = [(ts.value/1000000000000) for ts in tss]
In [11]: df({'strs' : strs, 'vals' : vals})
Out[11]: 
                         strs     vals
0   2012-01-02 01:00:00+00:00  1325466
1   2012-01-01 20:00:00-05:00  1325466
2   2012-01-01 20:00:00-05:00  1325466
3   2012-01-01 20:00:00-05:00  1325466
4   2012-01-01 19:00:00-06:00  1325466
5   2012-01-01 20:00:00-05:00  1325466
6   2012-01-01 20:00:00-05:00  1325466
7   2012-01-01 20:00:00-05:00  1325466
8   2012-01-01 19:00:00-06:00  1325466
9   2012-01-01 20:00:00-05:00  1325466
10  2012-01-01 20:00:00-05:00  1325466
11  2012-01-02 01:00:00+00:00  1325466
12  2012-01-01 20:00:00-05:00  1325466

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

No branches or pull requests

2 participants