コード例 #1
0
ファイル: test_panel.py プロジェクト: greeness/pandas
    def test_get_dummies(self):
        from pandas.core.reshape import make_column_dummies, make_axis_dummies

        self.panel["Label"] = self.panel.index.labels[1]
        minor_dummies = make_axis_dummies(self.panel, "minor")
        dummies = make_column_dummies(self.panel, "Label")
        self.assert_(np.array_equal(dummies.values, minor_dummies.values))
コード例 #2
0
ファイル: test_panel.py プロジェクト: theandygross/pandas
    def test_get_dummies(self):
        from pandas.core.reshape import make_column_dummies, make_axis_dummies

        self.panel['Label'] = self.panel.index.labels[1]
        minor_dummies = make_axis_dummies(self.panel, 'minor')
        dummies = make_column_dummies(self.panel, 'Label')
        self.assert_(np.array_equal(dummies.values, minor_dummies.values))
コード例 #3
0
    def _add_categorical_dummies(self, panel, cat_mappings):
        """
        Add categorical dummies to panel

        Returns
        -------
        DataFrame
        """
        from pandas.core.reshape import make_column_dummies

        if not self._x_effects:
            return panel

        dropped_dummy = (self._entity_effects and not self._use_all_dummies)

        for effect in self._x_effects:
            self.log('-- Adding fixed effect dummies for %s' % effect)

            dummies = make_column_dummies(panel, effect, prefix=False)

            val_map = cat_mappings.get(effect)
            if val_map:
                val_map = dict((v, k) for k, v in val_map.iteritems())

            if dropped_dummy or not self._use_all_dummies:
                if effect in self._dropped_dummies:
                    to_exclude = mapped_name = self._dropped_dummies.get(
                        effect)

                    if val_map:
                        mapped_name = val_map[to_exclude]
                else:
                    to_exclude = mapped_name = dummies.columns[0]

                if mapped_name not in dummies.columns:  # pragma: no cover
                    raise Exception('%s not in %s' %
                                    (to_exclude, dummies.columns))

                self.log('-- Excluding dummy for %s: %s' %
                         (effect, to_exclude))

                dummies = dummies.filter(dummies.columns - [mapped_name])
                dropped_dummy = True

            dummies = _convertDummies(dummies, cat_mappings.get(effect))
            dummies = dummies.add_prefix('%s_' % effect)
            panel = panel.join(dummies)

        return panel
コード例 #4
0
ファイル: plm.py プロジェクト: afonit/pandas
    def _add_categorical_dummies(self, panel, cat_mappings):
        """
        Add categorical dummies to panel

        Returns
        -------
        DataFrame
        """
        from pandas.core.reshape import make_column_dummies

        if not self._x_effects:
            return panel

        dropped_dummy = (self._entity_effects and not self._use_all_dummies)

        for effect in self._x_effects:
            self.log('-- Adding fixed effect dummies for %s' % effect)

            dummies = make_column_dummies(panel, effect, prefix=False)

            val_map = cat_mappings.get(effect)
            if val_map:
                val_map = dict((v, k) for k, v in val_map.iteritems())

            if dropped_dummy or not self._use_all_dummies:
                if effect in self._dropped_dummies:
                    to_exclude = mapped_name = self._dropped_dummies.get(effect)

                    if val_map:
                        mapped_name = val_map[to_exclude]
                else:
                    to_exclude = mapped_name = dummies.columns[0]

                if mapped_name not in dummies.columns: # pragma: no cover
                    raise Exception('%s not in %s' % (to_exclude,
                                                      dummies.columns))

                self.log('-- Excluding dummy for %s: %s' % (effect, to_exclude))

                dummies = dummies.filter(dummies.columns - [mapped_name])
                dropped_dummy = True

            dummies = _convertDummies(dummies, cat_mappings.get(effect))
            dummies = dummies.add_prefix('%s_' % effect)
            panel = panel.join(dummies)

        return panel