Skip to content

Commit 32252e1

Browse files
committed
Merge remote-tracking branch 'refs/remotes/pydata/master' into Fix-for-pandas-dev#11317
2 parents 486cc43 + 472e6e0 commit 32252e1

File tree

6 files changed

+81
-4
lines changed

6 files changed

+81
-4
lines changed

doc/source/whatsnew/v0.17.1.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Highlights include:
1717

1818
Enhancements
1919
~~~~~~~~~~~~
20+
- ``DatetimeIndex`` now supports conversion to strings with astype(str)(:issue:`10442`)
2021

2122
- Support for ``compression`` (gzip/bz2) in :method:`DataFrame.to_csv` (:issue:`7615`)
2223

@@ -64,8 +65,8 @@ Bug Fixes
6465

6566
- Bug in ``HDFStore.select`` when comparing with a numpy scalar in a where clause (:issue:`11283`)
6667

67-
- Bug in tz-conversions with an ambiguous time and ``.dt`` accessors (:issues:`11295`)
68-
68+
- Bug in tz-conversions with an ambiguous time and ``.dt`` accessors (:issue:`11295`)
69+
- Bug in comparisons of Series vs list-likes (:issue:`11339`)
6970

7071
- Bug in list-like indexing with a mixed-integer Index (:issue:`11320`)
7172

pandas/core/ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ def wrapper(self, other, axis=None):
720720
res = op(self.values, other)
721721
else:
722722
values = self.get_values()
723-
if is_list_like(other):
723+
if isinstance(other, (list, np.ndarray)):
724724
other = np.asarray(other)
725725

726726
res = na_op(values, other)

pandas/tests/test_series.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4269,6 +4269,43 @@ def test_object_comparisons(self):
42694269
expected = -(s == 'a')
42704270
assert_series_equal(result, expected)
42714271

4272+
def test_comparison_tuples(self):
4273+
# GH11339
4274+
# comparisons vs tuple
4275+
s = Series([(1,1),(1,2)])
4276+
4277+
result = s == (1,2)
4278+
expected = Series([False,True])
4279+
assert_series_equal(result, expected)
4280+
4281+
result = s != (1,2)
4282+
expected = Series([True, False])
4283+
assert_series_equal(result, expected)
4284+
4285+
result = s == (0,0)
4286+
expected = Series([False, False])
4287+
assert_series_equal(result, expected)
4288+
4289+
result = s != (0,0)
4290+
expected = Series([True, True])
4291+
assert_series_equal(result, expected)
4292+
4293+
s = Series([(1,1),(1,1)])
4294+
4295+
result = s == (1,1)
4296+
expected = Series([True, True])
4297+
assert_series_equal(result, expected)
4298+
4299+
result = s != (1,1)
4300+
expected = Series([False, False])
4301+
assert_series_equal(result, expected)
4302+
4303+
s = Series([frozenset([1]),frozenset([1,2])])
4304+
4305+
result = s == frozenset([1])
4306+
expected = Series([True, False])
4307+
assert_series_equal(result, expected)
4308+
42724309
def test_comparison_operators_with_nas(self):
42734310
s = Series(bdate_range('1/1/2000', periods=10), dtype=object)
42744311
s[::2] = np.nan

pandas/tseries/index.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,8 @@ def astype(self, dtype):
756756
return self.asi8.copy()
757757
elif dtype == _NS_DTYPE and self.tz is not None:
758758
return self.tz_convert('UTC').tz_localize(None)
759+
elif dtype == str:
760+
return self._shallow_copy(values=self.format(), infer=True)
759761
else: # pragma: no cover
760762
raise ValueError('Cannot cast DatetimeIndex to dtype %s' % dtype)
761763

pandas/tseries/tests/test_base.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,32 @@ def test_ops_properties_basic(self):
4545
self.assertEqual(s.day,10)
4646
self.assertRaises(AttributeError, lambda : s.weekday)
4747

48+
def test_astype_str(self):
49+
# test astype string - #10442
50+
result = date_range('2012-01-01', periods=4, name='test_name').astype(str)
51+
expected = Index(['2012-01-01', '2012-01-02', '2012-01-03','2012-01-04'],
52+
name='test_name', dtype=object)
53+
tm.assert_index_equal(result, expected)
54+
55+
# test astype string with tz and name
56+
result = date_range('2012-01-01', periods=3, name='test_name', tz='US/Eastern').astype(str)
57+
expected = Index(['2012-01-01 00:00:00-05:00', '2012-01-02 00:00:00-05:00',
58+
'2012-01-03 00:00:00-05:00'], name='test_name', dtype=object)
59+
tm.assert_index_equal(result, expected)
60+
61+
# test astype string with freqH and name
62+
result = date_range('1/1/2011', periods=3, freq='H', name='test_name').astype(str)
63+
expected = Index(['2011-01-01 00:00:00', '2011-01-01 01:00:00', '2011-01-01 02:00:00'],
64+
name='test_name', dtype=object)
65+
tm.assert_index_equal(result, expected)
66+
67+
# test astype string with freqH and timezone
68+
result = date_range('3/6/2012 00:00', periods=2, freq='H',
69+
tz='Europe/London', name='test_name').astype(str)
70+
expected = Index(['2012-03-06 00:00:00+00:00', '2012-03-06 01:00:00+00:00'],
71+
dtype=object, name='test_name')
72+
tm.assert_index_equal(result, expected)
73+
4874
def test_asobject_tolist(self):
4975
idx = pd.date_range(start='2013-01-01', periods=4, freq='M', name='idx')
5076
expected_list = [pd.Timestamp('2013-01-31'), pd.Timestamp('2013-02-28'),
@@ -503,7 +529,6 @@ def test_infer_freq(self):
503529
tm.assert_index_equal(idx, result)
504530
self.assertEqual(result.freq, freq)
505531

506-
507532
class TestTimedeltaIndexOps(Ops):
508533

509534
def setUp(self):

pandas/tseries/tests/test_timeseries.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,7 @@ def test_append_join_nondatetimeindex(self):
22232223
# it works
22242224
rng.join(idx, how='outer')
22252225

2226+
22262227
def test_astype(self):
22272228
rng = date_range('1/1/2000', periods=10)
22282229

@@ -2235,6 +2236,17 @@ def test_astype(self):
22352236
expected = date_range('1/1/2000', periods=10, tz='US/Eastern').tz_convert('UTC').tz_localize(None)
22362237
tm.assert_index_equal(result, expected)
22372238

2239+
# BUG#10442 : testing astype(str) is correct for Series/DatetimeIndex
2240+
result = pd.Series(pd.date_range('2012-01-01', periods=3)).astype(str)
2241+
expected = pd.Series(['2012-01-01', '2012-01-02', '2012-01-03'], dtype=object)
2242+
tm.assert_series_equal(result, expected)
2243+
2244+
result = Series(pd.date_range('2012-01-01', periods=3, tz='US/Eastern')).astype(str)
2245+
expected = Series(['2012-01-01 00:00:00-05:00', '2012-01-02 00:00:00-05:00', '2012-01-03 00:00:00-05:00'],
2246+
dtype=object)
2247+
tm.assert_series_equal(result, expected)
2248+
2249+
22382250
def test_to_period_nofreq(self):
22392251
idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-04'])
22402252
self.assertRaises(ValueError, idx.to_period)

0 commit comments

Comments
 (0)