This is a two-part question, with an immediate question and a more general one.
I have a pandas TimeSeries, ts.
To know the first value after a certain time. I can do this,
ts.ix[ts[datetime(2012,1,1,15,0,0):].first_valid_index()]
a) Is there a better, less clunky way to do it?
b) Coming from C, I have a certain phobia when dealing with these somewhat opaque, possibly mutable but generally not, possibly lazy but not always types. So to be clear, when I do
ts[datetime(2012,1,1,15,0,0):].first_valid_index()
ts[datetime(2012,1,1,15,0,0):] is a pandas.TimeSeries object right? And I could possibly mutate it.
Does it mean that whenever I take a slice, there’s a copy of ts being allocated in memory? Does it mean that this innocuous line of code could actually trigger the copy of a gigabyte of TimeSeries just to get an index value?
Or perhaps they magically share memory and a lazy copy is done if one of the object is mutated for instance? But then, how do you know which specific operations trigger a copy? Maybe not slicing but how about renaming columns? It doesn’t seem to say so in the documentation. Does that bother you? Should it bother me or should I just learn not to worry and catch problems with a profiler?
Some setup:
Okay, now to answer your first question, a) yes, there are less clunky ways, depending on your intention. This is pretty simple:
This is a slice containing all the values after your chosen date. You can select just the first one, as you wanted, by:
To your second question, (b) — this type of indexing is a slice of the original, just as other numpy arrays. It is NOT a copy of the original. See this question, or many similar:
Bug or feature: cloning a numpy array w/ slicing
To demonstrate, let’s modify the slice:
This changes the original timeseries object ts, since ts2 is a slice and not a copy.
If you DO want a copy, you can (in general) use the copy method or, (in this case) use truncate:
Changing this copy will not change the original.
This example is straight out of “Python for Data Analysis” by Wes. Check it out. It’s great.