ENH: support "nrows" and "chunksize" together
State: closed

 - [x] closes #15755
 - [x] tests added / passed
 - [x] passes ``git diff master | flake8 --diff`` (except for ``whatsnew/v0.20.0.txt``, I don't know why)
 - [x] whatsnew entry


Labels:
name: Enhancement
name: IO CSV
description: read_csv, to_csv

Comments:
actually I think we need some tests if chunksize > nrows.

and with get_chunk with nrows & chunksize specified. (mostly what happens at the edge point).
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/15756?src=pr&el=h1) Report
> Merging [#15756](https://codecov.io/gh/pandas-dev/pandas/pull/15756?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/1e753d7ce9dca129d7ec5383612f874ee1393788?src=pr&el=desc) will **decrease** coverage by `0.01%`.
> The diff coverage is `100%`.


```diff
@@            Coverage Diff             @@
##           master   #15756      +/-   ##
==========================================
- Coverage   91.01%      91%   -0.02%     
==========================================
  Files         143      143              
  Lines       49371    49370       -1     
==========================================
- Hits        44937    44928       -9     
- Misses       4434     4442       +8
```


| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/15756?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/io/parsers.py](https://codecov.io/gh/pandas-dev/pandas/compare/1e753d7ce9dca129d7ec5383612f874ee1393788...d0288e39ace949d5456d6d16b7e6dbca98067773?src=pr&el=tree#diff-cGFuZGFzL2lvL3BhcnNlcnMucHk=) | `95.51% <100%> (-0.01%)` | :arrow_down: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/compare/1e753d7ce9dca129d7ec5383612f874ee1393788...d0288e39ace949d5456d6d16b7e6dbca98067773?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `25% <0%> (-58.34%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/compare/1e753d7ce9dca129d7ec5383612f874ee1393788...d0288e39ace949d5456d6d16b7e6dbca98067773?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.86% <0%> (-0.1%)` | :arrow_down: |
| [pandas/core/common.py](https://codecov.io/gh/pandas-dev/pandas/compare/1e753d7ce9dca129d7ec5383612f874ee1393788...d0288e39ace949d5456d6d16b7e6dbca98067773?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvY29tbW9uLnB5) | `91.3% <0%> (+0.33%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/15756?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/15756?src=pr&el=footer). Last update [1e753d7...d0288e3](https://codecov.io/gh/pandas-dev/pandas/compare/1e753d7ce9dca129d7ec5383612f874ee1393788...d0288e39ace949d5456d6d16b7e6dbca98067773?el=footer&src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
> can you also add some tests for nrows/chunksize that they must be positive integers?

shall we cast floats (current behavior, as of #10476), or raise?
> > can you also add some tests for nrows/chunksize that they must be positive integers?

>shall we cast floats (current behavior, as of #10476), or raise?

nrows already has a validation function (which casts to float if possible), we can probably do the same for chunksize
There is currently actually a memory leak/infinite loop when doing eg `chunksize=0.5`, so would be good to validate `chunksize` the same as nrows. 

Negative values for nrows/chunksize currently return an empty frame, so which is kind of valid. If we want to change that, I would keep that for another PR/issue.
> nrows already has a validation function (which casts to float if possible), we can probably do the same for chunksize

yep that's a good idea
> nrows already has a validation function (which casts to float if possible), we can probably do the same for chunksize

Agree, but since chunksize=0 easily results, as @jorisvandenbossche mentioned, in infinite loops, I would impose chunksize >= 1. And maybe for (partial - asking nrows=0 is fine) coherence we want to impose nrows >= 0?

(I would like this, because negative chunksize/nrows could be misinterpreted as parsing starting from the end...)

In the meanwhile, I have pushed the requested tests for the original edit, in case we want to move the rest/discussion to another PR
> Negative values for nrows/chunksize currently return an empty frame, so which is kind of valid. If we want to change that, I would keep that for another PR/issue.

I think this is kind of wonky. We don't actually support indexing from the *end* of the file. I would raise on this. (can do in another independent issue).
cc @gfyoung if you can have a look.
LGTM!
thanks @toobaz 
You're welcome!