4

Newbie question: is there an in-place version of pandas truncate?

For example

>>> df = pandas.DataFrame({'stuff':range(5)}, index=range(5))
>>> df.truncate(2,3)

returns a new truncated data frame. I could do

>>> df = pandas.DataFrame({'stuff':range(5)}, index=range(5))
>>> df = df.truncate(2,3)

but that seems inefficient. Is there a way to do a more efficient truncate which does not make a copy but does the truncate in-place? Or is this kind of efficiency not an issue due to some uber-clever pandas design?

Note, that the docstring for truncate says there is a copy option, but I do not think that affects whether the dataframe itself is truncated but whether the returned value is a copy of the data frame or a reference to a portion of the data frame. See below for what I mean:

>>> df = pandas.DataFrame({'stuff':range(5)}, index=range(5))
>>> cp = df.truncate(2,3, copy=False)
>>> df
   stuff
0      0
1      1
2      2
3      3
4      4
>>> cp['stuff'][2] = -50
>>> df
   stuff
0      0
1      1
2    -50
3      3
4      4

Notice that when we use copy=False df is still not truncated but the returned object is a slice of df which when modified also modifies df.

Thanks.

Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
oxer
  • 975
  • 1
  • 10
  • 16

1 Answers1

2

There is no truncate in-place, but the inefficiency associated with making the copy can be avoided using copy=False (if the copy can be avoided at all, which is not always the case):

df = df.truncate(2,3, copy=False)

If you keep a reference to the original object it will remain unchanged though, as you discovered.

Note that in-place operations and copies are actually unrelated. In-place operations can copy the data and update some internal reference while mnethods that leave the object unchanged do not always have to copy the data.

Stop harming Monica
  • 12,141
  • 1
  • 36
  • 56