Skip to content

Commit 91ad4fd

Browse files
author
MomIsBestFriend
committed
Merge remote-tracking branch 'upstream/master' into STY-repr-doc
2 parents 4b5ada8 + 8d5e778 commit 91ad4fd

File tree

2 files changed

+46
-37
lines changed

2 files changed

+46
-37
lines changed

ci/print_skipped.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
def main(filename):
77
if not os.path.isfile(filename):
8-
raise RuntimeError(f"Could not find junit file {filename!r}")
8+
raise RuntimeError(f"Could not find junit file {repr(filename)}")
99

1010
tree = et.parse(filename)
1111
root = tree.getroot()

pandas/io/pytables.py

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55

66
import copy
7-
from datetime import date
7+
from datetime import date, tzinfo
88
import itertools
99
import os
1010
import re
@@ -2918,7 +2918,8 @@ def read_array(
29182918
if dtype == "datetime64":
29192919

29202920
# reconstruct a timezone if indicated
2921-
ret = _set_tz(ret, getattr(attrs, "tz", None), coerce=True)
2921+
tz = getattr(attrs, "tz", None)
2922+
ret = _set_tz(ret, tz, coerce=True)
29222923

29232924
elif dtype == "timedelta64":
29242925
ret = np.asarray(ret, dtype="m8[ns]")
@@ -3734,7 +3735,7 @@ def read_axes(
37343735

37353736
return True
37363737

3737-
def get_object(self, obj):
3738+
def get_object(self, obj, transposed: bool):
37383739
""" return the data for this obj """
37393740
return obj
37403741

@@ -3838,15 +3839,13 @@ def create_axes(
38383839
)
38393840

38403841
# create according to the new data
3841-
self.non_index_axes = []
3842-
self.data_columns = []
3842+
new_non_index_axes: List = []
3843+
new_data_columns: List[Optional[str]] = []
38433844

38443845
# nan_representation
38453846
if nan_rep is None:
38463847
nan_rep = "nan"
38473848

3848-
self.nan_rep = nan_rep
3849-
38503849
# create axes to index and non_index
38513850
index_axes_map = dict()
38523851
for i, a in enumerate(obj.axes):
@@ -3863,7 +3862,7 @@ def create_axes(
38633862
# necessary
38643863
append_axis = list(a)
38653864
if existing_table is not None:
3866-
indexer = len(self.non_index_axes)
3865+
indexer = len(new_non_index_axes)
38673866
exist_axis = existing_table.non_index_axes[indexer][1]
38683867
if not array_equivalent(
38693868
np.array(append_axis), np.array(exist_axis)
@@ -3880,34 +3879,37 @@ def create_axes(
38803879
info["names"] = list(a.names)
38813880
info["type"] = type(a).__name__
38823881

3883-
self.non_index_axes.append((i, append_axis))
3882+
new_non_index_axes.append((i, append_axis))
3883+
3884+
self.non_index_axes = new_non_index_axes
38843885

38853886
# set axis positions (based on the axes)
38863887
new_index_axes = [index_axes_map[a] for a in axes]
38873888
for j, iax in enumerate(new_index_axes):
38883889
iax.set_pos(j)
38893890
iax.update_info(self.info)
3890-
self.index_axes = new_index_axes
38913891

3892-
j = len(self.index_axes)
3892+
j = len(new_index_axes)
38933893

38943894
# check for column conflicts
3895-
for a in self.axes:
3895+
for a in new_index_axes:
38963896
a.maybe_set_size(min_itemsize=min_itemsize)
38973897

38983898
# reindex by our non_index_axes & compute data_columns
3899-
for a in self.non_index_axes:
3899+
for a in new_non_index_axes:
39003900
obj = _reindex_axis(obj, a[0], a[1])
39013901

39023902
def get_blk_items(mgr, blocks):
39033903
return [mgr.items.take(blk.mgr_locs) for blk in blocks]
39043904

3905+
transposed = new_index_axes[0].axis == 1
3906+
39053907
# figure out data_columns and get out blocks
3906-
block_obj = self.get_object(obj)._consolidate()
3908+
block_obj = self.get_object(obj, transposed)._consolidate()
39073909
blocks = block_obj._data.blocks
39083910
blk_items = get_blk_items(block_obj._data, blocks)
3909-
if len(self.non_index_axes):
3910-
axis, axis_labels = self.non_index_axes[0]
3911+
if len(new_non_index_axes):
3912+
axis, axis_labels = new_non_index_axes[0]
39113913
data_columns = self.validate_data_columns(data_columns, min_itemsize)
39123914
if len(data_columns):
39133915
mgr = block_obj.reindex(
@@ -3945,7 +3947,7 @@ def get_blk_items(mgr, blocks):
39453947
blk_items = new_blk_items
39463948

39473949
# add my values
3948-
self.values_axes = []
3950+
vaxes = []
39493951
for i, (b, b_items) in enumerate(zip(blocks, blk_items)):
39503952

39513953
# shape of the data column are the indexable axes
@@ -3959,7 +3961,7 @@ def get_blk_items(mgr, blocks):
39593961
if not (name is None or isinstance(name, str)):
39603962
# TODO: should the message here be more specifically non-str?
39613963
raise ValueError("cannot have non-object label DataIndexableCol")
3962-
self.data_columns.append(name)
3964+
new_data_columns.append(name)
39633965

39643966
# make sure that we match up the existing columns
39653967
# if we have an existing table
@@ -3987,10 +3989,15 @@ def get_blk_items(mgr, blocks):
39873989
)
39883990
col.set_pos(j)
39893991

3990-
self.values_axes.append(col)
3992+
vaxes.append(col)
39913993

39923994
j += 1
39933995

3996+
self.nan_rep = nan_rep
3997+
self.data_columns = new_data_columns
3998+
self.values_axes = vaxes
3999+
self.index_axes = new_index_axes
4000+
39944001
# validate our min_itemsize
39954002
self.validate_min_itemsize(min_itemsize)
39964003

@@ -4158,7 +4165,7 @@ def read_column(
41584165
encoding=self.encoding,
41594166
errors=self.errors,
41604167
)
4161-
return Series(_set_tz(a.take_data(), a.tz, True), name=column)
4168+
return Series(_set_tz(a.take_data(), a.tz), name=column)
41624169

41634170
raise KeyError(f"column [{column}] not found in the table")
41644171

@@ -4428,9 +4435,9 @@ class AppendableFrameTable(AppendableTable):
44284435
def is_transposed(self) -> bool:
44294436
return self.index_axes[0].axis == 1
44304437

4431-
def get_object(self, obj):
4438+
def get_object(self, obj, transposed: bool):
44324439
""" these are written transposed """
4433-
if self.is_transposed:
4440+
if transposed:
44344441
obj = obj.T
44354442
return obj
44364443

@@ -4512,7 +4519,7 @@ class AppendableSeriesTable(AppendableFrameTable):
45124519
def is_transposed(self) -> bool:
45134520
return False
45144521

4515-
def get_object(self, obj):
4522+
def get_object(self, obj, transposed: bool):
45164523
return obj
45174524

45184525
def write(self, obj, data_columns=None, **kwargs):
@@ -4687,37 +4694,39 @@ def _get_info(info, name):
46874694
# tz to/from coercion
46884695

46894696

4690-
def _get_tz(tz):
4697+
def _get_tz(tz: tzinfo) -> Union[str, tzinfo]:
46914698
""" for a tz-aware type, return an encoded zone """
46924699
zone = timezones.get_timezone(tz)
4693-
if zone is None:
4694-
zone = tz.utcoffset().total_seconds()
46954700
return zone
46964701

46974702

4698-
def _set_tz(values, tz, preserve_UTC: bool = False, coerce: bool = False):
4703+
def _set_tz(
4704+
values: Union[np.ndarray, Index],
4705+
tz: Optional[Union[str, tzinfo]],
4706+
coerce: bool = False,
4707+
) -> Union[np.ndarray, DatetimeIndex]:
46994708
"""
47004709
coerce the values to a DatetimeIndex if tz is set
47014710
preserve the input shape if possible
47024711
47034712
Parameters
47044713
----------
4705-
values : ndarray
4706-
tz : string/pickled tz object
4707-
preserve_UTC : bool,
4708-
preserve the UTC of the result
4714+
values : ndarray or Index
4715+
tz : str or tzinfo
47094716
coerce : if we do not have a passed timezone, coerce to M8[ns] ndarray
47104717
"""
4718+
if isinstance(values, DatetimeIndex):
4719+
# If values is tzaware, the tz gets dropped in the values.ravel()
4720+
# call below (which returns an ndarray). So we are only non-lossy
4721+
# if `tz` matches `values.tz`.
4722+
assert values.tz is None or values.tz == tz
4723+
47114724
if tz is not None:
47124725
name = getattr(values, "name", None)
47134726
values = values.ravel()
47144727
tz = timezones.get_timezone(_ensure_decoded(tz))
47154728
values = DatetimeIndex(values, name=name)
4716-
if values.tz is None:
4717-
values = values.tz_localize("UTC").tz_convert(tz)
4718-
if preserve_UTC:
4719-
if tz == "UTC":
4720-
values = list(values)
4729+
values = values.tz_localize("UTC").tz_convert(tz)
47214730
elif coerce:
47224731
values = np.asarray(values, dtype="M8[ns]")
47234732

0 commit comments

Comments
 (0)