Repository: pandas-dev/pandas

Issue:
DataFrame.head(0) doesn't return an empty frame
``` python
pandas.DataFrame({"stuff":[1,2,3,4,5]}).head(0)
```

returns

``` python
   stuff
0      1
1      2
2      3
3      4
4      5

```

I would expect an empty data frame, in the same way that

``` python
[1,2,3,4,5][0:0]
```

returns an empty list.

This is with pandas version 0.15.0.

The documentation (http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.DataFrame.head.html) says `head(n)` returns 'first n rows', so I'd expect 0 rows if n is 0.



Discussion:
The relevant line is https://github.com/pydata/pandas/blob/49b26600edf4c108a264c7982df8b28a46fac8b1/pandas/core/generic.py#L2135

``` python
    def head(self, n=5):
        """
        Returns first n rows
        """
        l = len(self)
        if l == 0 or n==0:
            return self
```

But  I don't see an explanation for this non-intuitive behaviour.

Looks like this was changed as a result of this issue: https://github.com/pydata/pandas/issues/5846

but don't really remember why this shouldn't return the same as `.iloc[:n]`. I suppose its prob more consistent to return the empty.

@TomAugspurger @hayd any thoughts here

returning an empty frame is probably best.

yep, so prob a propogated 'bug'.

ok, @trvrm want to do a pull-request for this. will be an API change (but tiny).

obviously `.tail` should be fixed as well.

we will need to confirm tests in `.groupby` as well (as these are also defined there)

+1


Resolving patch:
diff --git a/pandas/core/generic.py b/pandas/core/generic.py
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -2136,18 +2136,14 @@ def head(self, n=5):
         """
         Returns first n rows
         """
-        l = len(self)
-        if l == 0 or n==0:
-            return self
         return self.iloc[:n]
 
     def tail(self, n=5):
         """
         Returns last n rows
         """
-        l = len(self)
-        if l == 0 or n == 0:
-            return self
+        if n == 0:
+            return self.iloc[0:0]
         return self.iloc[-n:]
 
 


Test patch:
diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py
--- a/pandas/tests/test_frame.py
+++ b/pandas/tests/test_frame.py
@@ -5457,8 +5457,10 @@ def test_repr_column_name_unicode_truncation_bug(self):
     def test_head_tail(self):
         assert_frame_equal(self.frame.head(), self.frame[:5])
         assert_frame_equal(self.frame.tail(), self.frame[-5:])
-        assert_frame_equal(self.frame.head(0), self.frame)
-        assert_frame_equal(self.frame.tail(0), self.frame)
+         
+        assert_frame_equal(self.frame.head(0), self.frame[0:0])
+        assert_frame_equal(self.frame.tail(0), self.frame[0:0])
+        
         assert_frame_equal(self.frame.head(-1), self.frame[:-1])
         assert_frame_equal(self.frame.tail(-1), self.frame[1:])
         assert_frame_equal(self.frame.head(1), self.frame[:1])
@@ -5468,8 +5470,8 @@ def test_head_tail(self):
         df.index = np.arange(len(self.frame)) + 0.1
         assert_frame_equal(df.head(), df.iloc[:5])
         assert_frame_equal(df.tail(), df.iloc[-5:])
-        assert_frame_equal(df.head(0), df)
-        assert_frame_equal(df.tail(0), df)
+        assert_frame_equal(df.head(0), df[0:0])
+        assert_frame_equal(df.tail(0), df[0:0])
         assert_frame_equal(df.head(-1), df.iloc[:-1])
         assert_frame_equal(df.tail(-1), df.iloc[1:])
         #test empty dataframe
diff --git a/pandas/tests/test_generic.py b/pandas/tests/test_generic.py
--- a/pandas/tests/test_generic.py
+++ b/pandas/tests/test_generic.py
@@ -342,8 +342,8 @@ def test_head_tail(self):
             self._compare(o.tail(), o.iloc[-5:])
 
             # 0-len
-            self._compare(o.head(0), o.iloc[:])
-            self._compare(o.tail(0), o.iloc[0:])
+            self._compare(o.head(0), o.iloc[0:0])
+            self._compare(o.tail(0), o.iloc[0:0])
 
             # bounded
             self._compare(o.head(len(o)+1), o)
diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py
--- a/pandas/tests/test_series.py
+++ b/pandas/tests/test_series.py
@@ -7642,8 +7642,9 @@ def test_sortlevel(self):
 
     def test_head_tail(self):
         assert_series_equal(self.series.head(), self.series[:5])
+        assert_series_equal(self.series.head(0), self.series[0:0])
         assert_series_equal(self.series.tail(), self.series[-5:])
-
+        assert_series_equal(self.series.tail(0), self.series[0:0])
     def test_isin(self):
         s = Series(['A', 'B', 'C', 'a', 'B', 'B', 'A', 'C'])