Repository: pandas-dev/pandas
Pull request: BUG: indexing regression with datetime index
The bug is already fixed on master. I have just added a unit test.

- [x] closes #34860
- [x] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`


Problem statement:
BUG: indexing regression with datetime index
Sample code:
```python
import pandas as pd

idx = pd.date_range('2008', '2009')
idx.to_series()['2008']
```

in 0.25.3 we get
````
2008-01-01   2008-01-01
2008-01-02   2008-01-02
2008-01-03   2008-01-03
2008-01-04   2008-01-04
2008-01-05   2008-01-05
                ...    
2008-12-27   2008-12-27
2008-12-28   2008-12-28
2008-12-29   2008-12-29
2008-12-30   2008-12-30
2008-12-31   2008-12-31
Freq: D, Length: 366, dtype: datetime64[ns]
````

in >= 1.0.0 (up to 1.0.4 at least) we get
```
2008   2008-01-01
2008   2008-01-02
2008   2008-01-03
2008   2008-01-04
2008   2008-01-05
          ...    
2008   2008-12-27
2008   2008-12-28
2008   2008-12-29
2008   2008-12-30
2008   2008-12-31
Length: 366, dtype: datetime64[ns]
```

The index has dtype obj

Variantations like
```
idx.to_series().astype(str)['2008']
idx.to_series().loc['2008']
```
look unaffected

Hints from issue discussion:
Working as expected on master

```python
Out[1]:
2008-01-01   2008-01-01
2008-01-02   2008-01-02
2008-01-03   2008-01-03
2008-01-04   2008-01-04
2008-01-05   2008-01-05
                ...
2008-12-27   2008-12-27
2008-12-28   2008-12-28
2008-12-29   2008-12-29
2008-12-30   2008-12-30
2008-12-31   2008-12-31
Freq: D, Length: 366, dtype: datetime64[ns]
```

I don't recall which commit fixed it, but it'd be good to ensure that we have test coverage for this in `pandas/tests/series/indexing/test_datetime.py`.
@TomAugspurger
Thanks. I'll check to find the exact commit: it is fixed in master but not in 1.0.5
I'll add my example as a test case if you think that's a good idea
Interested in working on this issue!
Hi @faraz16iqbal !
Feel free to submit a pull request. [The contributing guide](https://pandas.pydata.org/pandas-docs/stable/development/contributing.html) is a good starting point.

Commit message: Removed index assert as it is duplicated by series assert

Patch to review:
diff --git a/pandas/tests/series/indexing/test_datetime.py b/pandas/tests/series/indexing/test_datetime.py
index 0b34fab7b80b1..836da68863cb7 100644
--- a/pandas/tests/series/indexing/test_datetime.py
+++ b/pandas/tests/series/indexing/test_datetime.py
@@ -667,3 +667,14 @@ def test_setitem_tuple_with_datetimetz():
     result[(0, 1)] = np.nan
     expected.iloc[0] = np.nan
     tm.assert_series_equal(result, expected)
+
+
+def test_indexing_regression():
+    # Issue 34860
+    arr = date_range("1/1/2008", "1/1/2009")
+    ser = arr.to_series()["2008"]
+
+    rng = date_range(start="2008-01-01", end="2008-12-31")
+    expected = Series(rng, index=rng)
+
+    tm.assert_series_equal(ser, expected)


Code review comments:
diff_hunk: @@ -667,3 +667,14 @@ def test_setitem_tuple_with_datetimetz():
     result[(0, 1)] = np.nan
     expected.iloc[0] = np.nan
     tm.assert_series_equal(result, expected)
+
+
+def test_indexing_regression():
+    # Issue 34860
+    arr = date_range("1/1/2008", "1/1/2009")
+    ser = arr.to_series()["2008"]
path: pandas/tests/series/indexing/test_datetime.py
text: @user1:
can you call this result

@author:
Sure, I can move the test to pandas/tests/indexing/test_partial.py location.
Could not understand your another comment. Could you please explain.

@author:
Got it, you mean rename `ser` to `result`. I will do that as well.