REGR: fix bug in DatetimeIndex slicing with irregular or unsorted indices
State: closed

closes #36953 
closes #35509
- [x] tests added / passed
- [ ] passes `black pandas`
- [ ] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [ ] whatsnew entry


Labels:
name: Datetime
description: Datetime data dtype
name: Indexing
description: Related to indexing on series/frames, not to indexes themselves
name: Regression
description: Functionality that used to work in a prior pandas version

Comments:
Hello @CloseChoice! Thanks for updating this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:





There are currently no PEP 8 issues detected in this Pull Request. Cheers! :beers: 

##### Comment last updated at 2020-10-26 12:55:55 UTC
> Thanks @CloseChoice for the PR!
> 
> Some comments.
> 
> Can you add the OP from #35509? As per [#36953 (comment)](https://github.com/pandas-dev/pandas/issues/36953#issuecomment-706220412) the two issues are caused by the same underlying bug but different behaviors so we'd like to have tests for both
> 
> I'm also not sure about the current location for this. Haven't looked in detail but somewhere like `pandas/tests/frame/test_timeseries.py` might be more appropriate

Hi @arw2019 thanks for the quick review. I added another test corresponding to #35509 and moved both tests to your suggested destination `pandas/tests/frame/test_timeseries.py`.
also this needs a whatsnew note for 1.1.4 in regressions (mention both issues)
> Thanks @CloseChoice lgtm pending green. are failures unrelated to the changes in this PR?

I don't see any connection between this PR and I see some of the failing tests in other PRs aswell. But might be worth an investigation.
k this looks fine, but I think there are some failing tests that are related: https://travis-ci.org/github/pandas-dev/pandas/jobs/737504953

so pls merge master and let's see what the CI says
@CloseChoice can you fix the pre-commit errors. (I'm on Windows and pre-commit fails to initialize and these checks can no longer be performed locally from code-checks.sh #37240)  cc @MarcoGorelli 
> @CloseChoice can you fix the pre-commit errors. (I'm on Windows and pre-commit fails to initialize and these checks can no longer be performed locally from code-checks.sh #37240) cc @MarcoGorelli

@CloseChoice you just need to replace `pd.DataFrame` with `DataFrame`

@simonjayhawkins what errors do you get? Part of the motivation for moving these checks over to pre-commit was that they'd be cross-platform

EDIT
----

@simonjayhawkins the pre-commit hooks all pass, it's the code-checks.sh that fail :rofl:
> @simonjayhawkins what errors do you get? 
i'm using a different machine, so have not looked in detail yet. (and can't remember what did last time)

<details>

### version information

```
pre-commit version: 2.7.1
sys.version:
    3.8.6 | packaged by conda-forge | (default, Oct  7 2020, 18:22:52) [MSC v.1916 64 bit (AMD64)]
sys.executable: C:\Users\simon\anaconda3\envs\pandas-dev\python.exe
os.name: nt
sys.platform: win32
```

### error information

```
An unexpected error has occurred: CalledProcessError: command: ('C:\\Users\\simon\\anaconda3\\envs\\pandas-dev\\python.exe', '-mvirtualenv', 'C:\\Users\\simon\\.cache\\pre-commit\\repoyew02fci\\py_env-python3', '-p', 'C:\\Users\\simon\\anaconda3\\envs\\pandas-dev\\python.exe')
return code: 1
expected return code: 0
stdout:
    FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\simon\\anaconda3\\envs\\pandas-dev\\Lib\\venv\\scripts\\nt\\python.exe'
    
stderr: (none)
```

```
Traceback (most recent call last):
  File "C:\Users\simon\anaconda3\envs\pandas-dev\lib\site-packages\pre_commit\error_handler.py", line 63, in error_handler
    yield
  File "C:\Users\simon\anaconda3\envs\pandas-dev\lib\site-packages\pre_commit\main.py", line 390, in main
    return run(args.config, store, args)
  File "C:\Users\simon\anaconda3\envs\pandas-dev\lib\site-packages\pre_commit\commands\run.py", line 388, in run
    install_hook_envs(hooks, store)
  File "C:\Users\simon\anaconda3\envs\pandas-dev\lib\site-packages\pre_commit\repository.py", line 206, in install_hook_envs
    _hook_install(hook)
  File "C:\Users\simon\anaconda3\envs\pandas-dev\lib\site-packages\pre_commit\repository.py", line 82, in _hook_install
    lang.install_environment(
  File "C:\Users\simon\anaconda3\envs\pandas-dev\lib\site-packages\pre_commit\languages\python.py", line 213, in install_environment
    cmd_output_b(*venv_cmd, cwd='/')
  File "C:\Users\simon\anaconda3\envs\pandas-dev\lib\site-packages\pre_commit\util.py", line 157, in cmd_output_b
    raise CalledProcessError(returncode, cmd, retcode, stdout_b, stderr_b)
pre_commit.util.CalledProcessError: command: ('C:\\Users\\simon\\anaconda3\\envs\\pandas-dev\\python.exe', '-mvirtualenv', 'C:\\Users\\simon\\.cache\\pre-commit\\repoyew02fci\\py_env-python3', '-p', 'C:\\Users\\simon\\anaconda3\\envs\\pandas-dev\\python.exe')
return code: 1
expected return code: 0
stdout:
    FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\simon\\anaconda3\\envs\\pandas-dev\\Lib\\venv\\scripts\\nt\\python.exe'
    
stderr: (none)

```

</details>

> Part of the motivation for moving these checks over to pre-commit was that they'd be cross-platform

I understand that, but didn't we discuss that pre-commit was optional.


> @simonjayhawkins the pre-commit hooks all pass, it's the code-checks.sh that fail 🤣

my bad. missed that.
@simonjayhawkins thanks for the traceback - are you using conda? I could reproduce that on a windows conda environment. Looks like it's a bug in virtualenv https://github.com/pypa/virtualenv/issues/1986

If you downgrade virtualenv to 20.0.33 then it should work (did for me) - if it works for you I'll note it in the docs
> If you downgrade virtualenv to 20.0.33 then it should work (did for me) - if it works for you I'll note it in the docs

thanks works for me too. (20.0.35 was installed). just started running on all-files. will report back if fails.
back to the issue

> so pls merge master and let's see what the CI says

failures on Windows py38_np18 seen on other PRs.
thanks @CloseChoice 
> will report back if fails.

although it installed fine for me, some hooks still failed, likely because currently we have
```
./scripts/validate_unwanted_patterns --validation-type="private_function_across_module"
```
instead of
```
python scripts/validate_unwanted_patterns --validation-type="private_function_across_module"
```
, will fix
> > will report back if fails.
> 
> although it installed fine for me, some hooks still failed, likely because currently we have

works for me in anaconda prompt and git bash