~ubuntu-branches/ubuntu/raring/pandas/raring

« back to all changes in this revision

Viewing changes to pandas/core/panel.py

  • Committer: Package Import Robot
  • Author(s): Yaroslav Halchenko
  • Date: 2012-07-22 20:13:16 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20120722201316-6v4zbottvreomrz7
Tags: 0.8.1-1
* Primarily a bugfix upstream release.
* up_tag_yahoo_test_requiring_network patch removed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
682
682
        major = _mut_exclusive(major, major_axis)
683
683
        minor = _mut_exclusive(minor, minor_axis)
684
684
 
 
685
        if (method is None and not self._is_mixed_type and
 
686
            com._count_not_none(items, major, minor) == 3):
 
687
            return self._reindex_multi(items, major, minor)
 
688
 
685
689
        if major is not None:
686
690
            result = result._reindex_axis(major, method, 1, copy)
687
691
 
696
700
 
697
701
        return result
698
702
 
 
703
    def _reindex_multi(self, items, major, minor):
 
704
        a0, a1, a2 = len(items), len(major), len(minor)
 
705
 
 
706
        values = self.values
 
707
        new_values = np.empty((a0, a1, a2), dtype=values.dtype)
 
708
 
 
709
        new_items, indexer0 = self.items.reindex(items)
 
710
        new_major, indexer1 = self.major_axis.reindex(major)
 
711
        new_minor, indexer2 = self.minor_axis.reindex(minor)
 
712
 
 
713
        if indexer0 is None:
 
714
            indexer0 = range(len(new_items))
 
715
 
 
716
        if indexer1 is None:
 
717
            indexer1 = range(len(new_major))
 
718
 
 
719
        if indexer2 is None:
 
720
            indexer2 = range(len(new_minor))
 
721
 
 
722
        for i, ind in enumerate(indexer0):
 
723
            com.take_2d_multi(values[ind], indexer1, indexer2,
 
724
                              out=new_values[i])
 
725
 
 
726
        return Panel(new_values, items=new_items, major_axis=new_major,
 
727
                     minor_axis=new_minor)
 
728
 
699
729
    def reindex_axis(self, labels, axis=0, method=None, level=None, copy=True):
700
730
        """Conform Panel to new index with optional filling logic, placing
701
731
        NA/NaN in locations having no value in the previous index. A new object
744
774
        return self.reindex(major=other.major_axis, items=other.items,
745
775
                            minor=other.minor_axis, method=method)
746
776
 
 
777
    def dropna(self, axis=0, how='any'):
 
778
        """
 
779
        Drop 2D from panel, holding passed axis constant
 
780
 
 
781
        Parameters
 
782
        ----------
 
783
        axis : int, default 0
 
784
            Axis to hold constant. E.g. axis=1 will drop major_axis entries
 
785
            having a certain amount of NA data
 
786
        how : {'all', 'any'}, default 'any'
 
787
            'any': one or more values are NA in the DataFrame along the
 
788
            axis. For 'all' they all must be.
 
789
 
 
790
        Returns
 
791
        -------
 
792
        dropped : Panel
 
793
        """
 
794
        axis = self._get_axis_number(axis)
 
795
 
 
796
        values = self.values
 
797
        mask = com.notnull(values)
 
798
 
 
799
        for ax in reversed(sorted(set(range(3)) - set([axis]))):
 
800
            mask = mask.sum(ax)
 
801
 
 
802
        per_slice = np.prod(values.shape[:axis] + values.shape[axis + 1:])
 
803
 
 
804
        if how == 'all':
 
805
            cond = mask > 0
 
806
        else:
 
807
            cond = mask == per_slice
 
808
 
 
809
        new_ax = self._get_axis(axis)[cond]
 
810
        return self.reindex_axis(new_ax, axis=axis)
 
811
 
747
812
    def _combine(self, other, func, axis=0):
748
813
        if isinstance(other, Panel):
749
814
            return self._combine_panel(other, func)
894
959
        new_data = self._data.xs(key, axis=axis_number, copy=copy)
895
960
        return DataFrame(new_data)
896
961
 
 
962
    def _ixs(self, i, axis=0):
 
963
        # for compatibility with .ix indexing
 
964
        # Won't work with hierarchical indexing yet
 
965
        key = self._get_axis(axis)[i]
 
966
        return self.xs(key, axis=axis)
 
967
 
897
968
    def groupby(self, function, axis='major'):
898
969
        """
899
970
        Group data on given axis, returning GroupBy object