bug (cannot check current version): dataframe slice [:N] contains N+1 element

This issue has been created since 2022-11-16.

Apologies, I cannot use the BUG template as pip install -U pandas does not go beyond version 1.1.5 on my system, presumably due to a version of Python you no longer support.

Reproducible Example:

import pandas
from io import StringIO

testdata = StringIO("""r_id,text,label
s1,a,n
s2,b,p
s3,c,n
s4,d,p
s5,e,n
""")

df = pandas.read_csv(testdata)
assert len(df) == 5
print(len(df.loc[:4]))

Issue Description: The slice has 5, not 4, items and includes the item s5.

Expected Behaviour: The slice contains the first 4 items only.

Installed versions:

INSTALLED VERSIONS

commit : b5958ee
python : 3.6.15.final.0
python-bits : 64
OS : Linux
OS-release : 5.3.18-150300.59.98-default
Version : #1 SMP Thu Oct 13 08:52:00 UTC 2022 (dfcde7e)
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 1.1.5
numpy : 1.19.5
pytz : 2022.6
dateutil : 2.8.2
pip : 21.3.1
setuptools : 59.6.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.5.4
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

MarcoGorelli wrote this answer on 2022-11-16

Thanks for your report - this is expected, see https://pandas.pydata.org/docs/user_guide/advanced.html#advanced-endpoints-are-inclusive

closing then

More Details About Repo
Owner Name pandas-dev
Repo Name pandas
Full Name pandas-dev/pandas
Language Python
Created Date 2010-08-24
Updated Date 2022-12-07
Star Count 36168
Watcher Count 1118
Fork Count 15472
Issue Count 3683

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Resume training fails with 'NoneType' object is not subscriptable 6 2022-07-04 2022-10-16
Admin console - assigning user roles 1 2021-10-05 2022-11-06
Recover from dead connections? 9 2021-09-22 2022-07-15
error while running main.py 5 2022-06-21 2022-07-18
Can't attack critters with spells 3 2022-06-18 2022-09-17
Add some more data sets to the package? 0 2022-02-19 2022-10-01
No such file or directory 4 2020-04-23 2022-09-06
TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first. 6 2020-09-23 2022-09-06
No module named 'lib.imdb_util' 2 2020-09-29 2022-09-06
CUDA out of memory error 1 2020-11-06 2022-09-06
CMake + git submodule 0 2021-07-04 2022-11-06
compiling error:no instance of overloaded function 2 2022-04-01 2022-10-03
unable to control integrated webcam on Surface GO3 2 2022-03-30 2022-11-04
Nodes on the instance data source is not computing the correct count 1 2021-09-15 2022-10-03
HTTP Streaming Protocol support? 4 2022-06-21 2022-11-14
Base.find(String query, Object... params) is fetching the whole resultset instead of streaming it 5 2021-04-16 2022-09-14
Bug - nothing happen when opening mailto link 4 2022-08-21 2022-11-08
--pre 0 is not working correctly 2 2021-07-14 2022-10-04
服务端DNS配置不生效,不能试用自定义DNS server 1 2022-07-01 2022-12-06
Structural Errors in documentation 0 2022-07-23 2022-12-06
[Question] Different between TempSwitchLayer1 and Layer1Button 3 2020-08-28 2022-10-31
subprocess.Popen 0 2022-01-09 2022-01-09
IllegalStateException: Did not find any type information for expression 1 2022-05-05 2022-11-20
UnsupportedOperationException: TEXT index has no value capability 2 2022-05-04 2022-11-13
[CRASH - Pokémon Y] Trainer/Wild editors 0 2021-09-20 2022-11-13
Should be able to detect or add behavior when there is a missing topic 3 2019-05-20 2022-09-26
the result of running test.py 3 2022-01-16 2022-10-02
unable to retrieve StnryAssetEnvSrcCnfg 5 2022-05-17 2022-07-22
Possibility to add more "Tokens used in the property list"? 2 2021-10-11 2022-12-04
[Broken Website] https://www.startech.com.bd/ 1 2022-05-13 2022-11-27
Update `webui` e2e tests documentation 0 2021-11-24 2022-10-12
Autodiscovery docs are incomplete 1 2019-12-31 2022-11-12
Worker threads not being not recreated. 1 2022-07-02 2022-12-04
Question: GPS and IMU support, multiple RGB-D cameras 6 2022-10-21 2022-12-02
cf7-cli rpm overwrites file without notice 3 2020-11-12 2022-10-26
Add MathML Core 0 2021-10-30 2022-01-09
Support projects without makefile 0 2012-10-29 2022-07-07
Misaligned context menus on Windows 8.1 8 2021-08-13 2022-10-23
NPM install shows oudated dependencies warnings 5 2021-08-30 2022-11-28
Move DefinitionStorage from yiisoft/di 0 2021-09-20 2022-10-29
how to control velocity of fw using mavsdk 10 2022-01-24 2022-10-11
Input helper 1 2020-07-07 2022-01-18
ClassLoader from jar for Coca 0 2020-07-13 2022-11-27
ernie inference 2 2021-07-27 2021-11-02
Target directory error when using Sha2::256::Digest as return 2 2022-01-27 2022-10-03
Current implementation of `--diff` is kind of broken by design 1 2021-09-16 2022-11-08
ide does not work it keeps loading and give me no thing 1 2022-11-25 2022-11-27
Options to personalize label & category 0 2018-11-08 2022-12-05
addresses/creditcards: Store error: JSON Error: missing field `entry` 3 2022-06-27 2022-10-07
[EuiButtonIcon] isLoading is not supported despite documentation 2 2022-03-10 2022-12-01