Repository: pandas-dev/pandas

Issue:
Series.apply fails when the series is a timezone aware datetime
To reproduce the problem:
```python
import pandas as pd

s = pd.util.testing.makeTimeSeries()
s = s.tz_localize('UTC')
pd.Series(s.index).apply(lambda x: pd.Series([1,2]))

AttributeError: 'PandasArray' object has no attribute 'tolist'
```
It works fine if the function to apply returns a scalar instead of a Series:

```python
import pandas as pd

s = pd.util.testing.makeTimeSeries()
s = s.tz_localize('UTC')
pd.Series(s.index).apply(lambda x: 1)
```
or it there is no timezone information in the series.
```python
import pandas as pd

s = pd.util.testing.makeTimeSeries()
pd.Series(s.index).apply(lambda x: pd.Series([1,2]))
```
The issue is present with pandas 0.24.2, it used to work fine with 0.23.4.
Series.apply fails when the series is a timezone aware datetime
To reproduce the problem:
```python
import pandas as pd

s = pd.util.testing.makeTimeSeries()
s = s.tz_localize('UTC')
pd.Series(s.index).apply(lambda x: pd.Series([1,2]))

AttributeError: 'PandasArray' object has no attribute 'tolist'
```
It works fine if the function to apply returns a scalar instead of a Series:

```python
import pandas as pd

s = pd.util.testing.makeTimeSeries()
s = s.tz_localize('UTC')
pd.Series(s.index).apply(lambda x: 1)
```
or it there is no timezone information in the series.
```python
import pandas as pd

s = pd.util.testing.makeTimeSeries()
pd.Series(s.index).apply(lambda x: pd.Series([1,2]))
```
The issue is present with pandas 0.24.2, it used to work fine with 0.23.4.


Discussion:
I'd like to pick this up as my first issue to contribute to this repo :)
I'd like to pick this up as my first issue to contribute to this repo :)

Resolving patch:
diff --git a/pandas/core/series.py b/pandas/core/series.py
--- a/pandas/core/series.py
+++ b/pandas/core/series.py
@@ -28,6 +28,7 @@
 from pandas.core.dtypes.missing import (
     isna, na_value_for_dtype, notna, remove_na_arraylike)
 
+import pandas as pd
 from pandas.core import algorithms, base, generic, nanops, ops
 from pandas.core.accessor import CachedAccessor
 from pandas.core.arrays import ExtensionArray, SparseArray
@@ -3702,8 +3703,10 @@ def f(x):
                 mapped = lib.map_infer(values, f, convert=convert_dtype)
 
         if len(mapped) and isinstance(mapped[0], Series):
-            from pandas.core.frame import DataFrame
-            return DataFrame(mapped.tolist(), index=self.index)
+            # GH 25959 use pd.array instead of tolist
+            # so extension arrays can be used
+            return self._constructor_expanddim(pd.array(mapped),
+                                               index=self.index)
         else:
             return self._constructor(mapped,
                                      index=self.index).__finalize__(self)


Test patch:
diff --git a/pandas/tests/series/test_apply.py b/pandas/tests/series/test_apply.py
--- a/pandas/tests/series/test_apply.py
+++ b/pandas/tests/series/test_apply.py
@@ -671,3 +671,23 @@ def test_map_missing_mixed(self, vals, mapping, exp):
         result = s.map(mapping)
 
         tm.assert_series_equal(result, pd.Series(exp))
+
+    @pytest.mark.parametrize("dti,exp", [
+        (Series([1, 2], index=pd.DatetimeIndex([0, 31536000000])),
+            DataFrame(np.repeat([[1, 2]], 2, axis=0), dtype='int64')),
+        (tm.makeTimeSeries(nper=30),
+            DataFrame(np.repeat([[1, 2]], 30, axis=0), dtype='int64'))
+    ])
+    def test_apply_series_on_date_time_index_aware_series(self, dti, exp):
+        # GH 25959
+        # Calling apply on a localized time series should not cause an error
+        index = dti.tz_localize('UTC').index
+        result = pd.Series(index).apply(lambda x: pd.Series([1, 2]))
+        assert_frame_equal(result, exp)
+
+    def test_apply_scaler_on_date_time_index_aware_series(self):
+        # GH 25959
+        # Calling apply on a localized time series should not cause an error
+        series = tm.makeTimeSeries(nper=30).tz_localize('UTC')
+        result = pd.Series(series.index).apply(lambda x: 1)
+        assert_series_equal(result, pd.Series(np.ones(30), dtype='int64'))