-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Inserting subclass/composition of Series into DataFrame strips 'extra' functions/properties #1713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
see also #60 In [44]: def say_hello(s):
....: print "hello"
....:
In [45]: def my_max(s):
....: print "my_max"
....:
In [46]: pandas.Series.say_hello = say_hello
In [47]: pandas.Series.max = my_max
In [48]: s = pandas.Series(np.random.rand(4), index=list('abcd'))
In [49]: s.say_hello()
hello
In [50]: s.max()
my_max
In [51]: df = pandas.DataFrame(np.random.randint(0, 10, (4,4)), index=list('abcd'), columns=list('ABCD'))
In [52]: df
Out[52]:
A B C D
a 6 0 9 5
b 9 1 1 3
c 1 9 6 6
d 8 4 8 7
In [53]: df['A'].say_hello()
hello
In [55]: df['A'].max()
my_max |
We need per-column metadata to be able to do what you're describing. I'm still unsure about the design |
Can you see if https://github.com/dalejung/pandas-composition works for you? |
This is excellent! Would solve pretty much any of the issues I can think of On Tue, Jul 16, 2013 at 10:20 PM, dalejung [email protected] wrote:
Dr. Carson J. Q. Farmer |
I'm glad you've found something that works, does that mean this is going to |
While I think pandas_composition is a 'solution' for now, per-column Carson On Thu, Jul 18, 2013 at 6:18 PM, Jeff Tratner [email protected]:
Dr. Carson J. Q. Farmer |
Right, missed that one. On Mon, Jul 22, 2013 at 1:58 PM, jreback [email protected] wrote:
Dr. Carson J. Q. Farmer |
If it helps, pandas_composition does have column meta data that persists. |
@jreback Now that I'm taking a closer look at your PR, I see all the lovely On Mon, Jul 22, 2013 at 2:29 PM, jreback [email protected] wrote:
Dr. Carson J. Q. Farmer |
sure.....(this is been in the works for a while FYI) |
Yes I see that now, wish I had seen this sooner! On Mon, Jul 22, 2013 at 2:50 PM, jreback [email protected] wrote:
Dr. Carson J. Q. Farmer |
ironically your pr caused me to rebase back to current (as this was written right about the time 0.11 release) (mostly)...so thanks! |
covered by #2485 |
When trying to insert/append a subclass (or composition) of a pandas Series into a DataFrame, any and all of the 'extra' functions that come with my subclass (or composition) are stripped and a Series is created:
I suspect that for the most part, this kind of behaviour is useful, however, I need the extra functions and classes associated with SpatialSeries, and I'd rather not have to subclass DataFrame to create a special DataFrame that allows this. It looks like the culprit is here in frame.py at lines 1761-1772:
I particular, I'm looking at
value = self._sanitize_column(key, value)
, which appears to usenp.asarray(value)
before it returns the input array (even if the input column is a Series). Is there any way to avoid this behaviour? Or alternatively, a better way to implement this so that useful subclasses can be used within a DataFrame? I hope I'm not missing something simple/vital here?FYI:
The text was updated successfully, but these errors were encountered: