Skip to content

BUG: Transpose fails on DataFrame with timestamps having timezone #17539

Closed
@pratapvardhan

Description

@pratapvardhan

Code

In [33]: df = pd.DataFrame({'date1': [Timestamp('2017-05-25 14:02:23'), NaT],
             'date2': [Timestamp('2017-05-25 14:34:43'), Timestamp('2017-05-16 19:37:43')]})

In [34]: df2 = df.apply(lambda x: x.dt.tz_localize('UTC'), axis=0)

In [35]: df3 = df2.assign(date1=df2.date2)

In [36]: df
Out[36]:
                date1               date2
0 2017-05-25 14:02:23 2017-05-25 14:34:43
1                 NaT 2017-05-16 19:37:43

In [37]: df2
Out[37]:
                      date1                     date2
0 2017-05-25 14:02:23+00:00 2017-05-25 14:34:43+00:00
1                       NaT 2017-05-16 19:37:43+00:00

In [38]: df3
Out[38]:
                      date1                     date2
0 2017-05-25 14:34:43+00:00 2017-05-25 14:34:43+00:00
1 2017-05-16 19:37:43+00:00 2017-05-16 19:37:43+00:00

In [39]: df.dtypes
Out[39]:
date1    datetime64[ns]
date2    datetime64[ns]
dtype: object

In [40]: df2.dtypes
Out[40]:
date1    datetime64[ns, UTC]
date2    datetime64[ns, UTC]
dtype: object

In [41]: df3.dtypes
Out[41]:
date1    datetime64[ns, UTC]
date2    datetime64[ns, UTC]
dtype: object

In [42]: df.T
Out[42]:
                        0                   1
date1 2017-05-25 14:02:23                 NaT
date2 2017-05-25 14:34:43 2017-05-16 19:37:43

In [43]: df2.T
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-43-7d2317a1beae> in <module>()
----> 1 df2.T

e:\github\pandas\pandas\core\frame.pyc in transpose(self, *args, **kwargs)
   1876         """Transpose index and columns"""
   1877         nv.validate_transpose(args, dict())
-> 1878         return super(DataFrame, self).transpose(1, 0, **kwargs)
   1879
   1880     T = property(transpose)

e:\github\pandas\pandas\core\generic.pyc in transpose(self, *args, **kwargs)
    600
    601         nv.validate_transpose_for_generic(self, kwargs)
--> 602         return self._constructor(new_values, **new_axes).__finalize__(self)
    603
    604     def swapaxes(self, axis1, axis2, copy=True):

e:\github\pandas\pandas\core\frame.pyc in __init__(self, data, index, columns, dtype, copy)
    350             else:
    351                 mgr = self._init_ndarray(data, index, columns, dtype=dtype,
--> 352                                          copy=copy)
    353         elif isinstance(data, (list, types.GeneratorType)):
    354             if isinstance(data, types.GeneratorType):

e:\github\pandas\pandas\core\frame.pyc in _init_ndarray(self, values, index, columns, dtype, copy)
    522             values = maybe_infer_to_datetimelike(values)
    523
--> 524         return create_block_manager_from_blocks([values], [columns, index])
    525
    526     @property

e:\github\pandas\pandas\core\internals.pyc in create_block_manager_from_blocks(blocks, axes)
   4378                                      placement=slice(0, len(axes[0])))]
   4379
-> 4380         mgr = BlockManager(blocks, axes)
   4381         mgr._consolidate_inplace()
   4382         return mgr

e:\github\pandas\pandas\core\internals.pyc in __init__(self, blocks, axes, do_integrity_check, fastpath)
   2880                     raise AssertionError('Number of Block dimensions (%d) '
   2881                                          'must equal number of axes (%d)' %
-> 2882                                          (block.ndim, self.ndim))
   2883
   2884         if do_integrity_check:

AssertionError: Number of Block dimensions (1) must equal number of axes (2)

Problem description

Transpose on dataframe with timestamps columns work. But, if columns consist of timestamps with time-zone, it fails.

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 2.7.12.final.0 python-bits: 64 OS: Windows OS-release: 7 machine: AMD64 processor: Intel64 Family 6 Model 61 Stepping 4, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: None.None

pandas: 0.21.0.dev+413.g7f93d2d
pytest: 3.2.0
pip: 9.0.1
setuptools: 36.2.7
Cython: 0.24.1
numpy: 1.12.1
scipy: 0.19.1
pyarrow: None
xarray: None
IPython: 5.1.0
sphinx: 1.4.6
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2017.2
blosc: None
bottleneck: 1.2.0
tables: 3.2.2
numexpr: 2.6.2
feather: None
matplotlib: 2.0.2
openpyxl: 2.3.2
xlrd: 1.0.0
xlwt: 1.1.2
xlsxwriter: 0.9.3
lxml: 3.6.4
bs4: 4.5.1
html5lib: 0.999999999
sqlalchemy: 1.0.13
pymysql: 0.7.9.None
psycopg2: 2.7.3.1 (dt dec pq3 ext lo64)
jinja2: 2.8
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    DatetimeDatetime data dtypeDuplicate ReportDuplicate issue or pull requestReshapingConcat, Merge/Join, Stack/Unstack, ExplodeTimezonesTimezone data dtype

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions