@@ -700,7 +700,7 @@ def group_last_%(name)s(ndarray[%(dest_type2)s, ndim=2] out,
700
700
Only aggregates on axis=0
701
701
'''
702
702
cdef:
703
- Py_ssize_t i, j, N, K, lab
703
+ Py_ssize_t i, j, N, K, lab, ncounts = len(counts)
704
704
%(dest_type2)s val, count
705
705
ndarray[%(dest_type2)s, ndim=2] resx
706
706
ndarray[int64_t, ndim=2] nobs
@@ -713,26 +713,27 @@ def group_last_%(name)s(ndarray[%(dest_type2)s, ndim=2] out,
713
713
714
714
N, K = (<object> values).shape
715
715
716
- for i in range(N):
717
- lab = labels[i]
718
- if lab < 0:
719
- continue
716
+ with nogil:
717
+ for i in range(N):
718
+ lab = labels[i]
719
+ if lab < 0:
720
+ continue
720
721
721
- counts[lab] += 1
722
- for j in range(K):
723
- val = values[i, j]
722
+ counts[lab] += 1
723
+ for j in range(K):
724
+ val = values[i, j]
724
725
725
- # not nan
726
- if val == val:
727
- nobs[lab, j] += 1
728
- resx[lab, j] = val
726
+ # not nan
727
+ if val == val:
728
+ nobs[lab, j] += 1
729
+ resx[lab, j] = val
729
730
730
- for i in range(len(counts) ):
731
- for j in range(K):
732
- if nobs[i, j] == 0:
733
- out[i, j] = %(nan_val)s
734
- else:
735
- out[i, j] = resx[i, j]
731
+ for i in range(ncounts ):
732
+ for j in range(K):
733
+ if nobs[i, j] == 0:
734
+ out[i, j] = %(nan_val)s
735
+ else:
736
+ out[i, j] = resx[i, j]
736
737
"""
737
738
738
739
group_last_bin_template = """@cython.wraparound(False)
@@ -759,26 +760,27 @@ def group_last_bin_%(name)s(ndarray[%(dest_type2)s, ndim=2] out,
759
760
760
761
N, K = (<object> values).shape
761
762
762
- b = 0
763
- for i in range(N):
764
- while b < ngroups - 1 and i >= bins[b]:
765
- b += 1
763
+ with nogil:
764
+ b = 0
765
+ for i in range(N):
766
+ while b < ngroups - 1 and i >= bins[b]:
767
+ b += 1
766
768
767
- counts[b] += 1
768
- for j in range(K):
769
- val = values[i, j]
769
+ counts[b] += 1
770
+ for j in range(K):
771
+ val = values[i, j]
770
772
771
- # not nan
772
- if val == val:
773
- nobs[b, j] += 1
774
- resx[b, j] = val
773
+ # not nan
774
+ if val == val:
775
+ nobs[b, j] += 1
776
+ resx[b, j] = val
775
777
776
- for i in range(ngroups):
777
- for j in range(K):
778
- if nobs[i, j] == 0:
779
- out[i, j] = %(nan_val)s
780
- else:
781
- out[i, j] = resx[i, j]
778
+ for i in range(ngroups):
779
+ for j in range(K):
780
+ if nobs[i, j] == 0:
781
+ out[i, j] = %(nan_val)s
782
+ else:
783
+ out[i, j] = resx[i, j]
782
784
"""
783
785
784
786
group_nth_bin_template = """@cython.wraparound(False)
@@ -805,27 +807,28 @@ def group_nth_bin_%(name)s(ndarray[%(dest_type2)s, ndim=2] out,
805
807
806
808
N, K = (<object> values).shape
807
809
808
- b = 0
809
- for i in range(N):
810
- while b < ngroups - 1 and i >= bins[b]:
811
- b += 1
810
+ with nogil:
811
+ b = 0
812
+ for i in range(N):
813
+ while b < ngroups - 1 and i >= bins[b]:
814
+ b += 1
812
815
813
- counts[b] += 1
814
- for j in range(K):
815
- val = values[i, j]
816
+ counts[b] += 1
817
+ for j in range(K):
818
+ val = values[i, j]
816
819
817
- # not nan
818
- if val == val:
819
- nobs[b, j] += 1
820
- if nobs[b, j] == rank:
821
- resx[b, j] = val
820
+ # not nan
821
+ if val == val:
822
+ nobs[b, j] += 1
823
+ if nobs[b, j] == rank:
824
+ resx[b, j] = val
822
825
823
- for i in range(ngroups):
824
- for j in range(K):
825
- if nobs[i, j] == 0:
826
- out[i, j] = %(nan_val)s
827
- else:
828
- out[i, j] = resx[i, j]
826
+ for i in range(ngroups):
827
+ for j in range(K):
828
+ if nobs[i, j] == 0:
829
+ out[i, j] = %(nan_val)s
830
+ else:
831
+ out[i, j] = resx[i, j]
829
832
"""
830
833
831
834
group_nth_template = """@cython.wraparound(False)
@@ -838,7 +841,7 @@ def group_nth_%(name)s(ndarray[%(dest_type2)s, ndim=2] out,
838
841
Only aggregates on axis=0
839
842
'''
840
843
cdef:
841
- Py_ssize_t i, j, N, K, lab
844
+ Py_ssize_t i, j, N, K, lab, ncounts = len(counts)
842
845
%(dest_type2)s val, count
843
846
ndarray[%(dest_type2)s, ndim=2] resx
844
847
ndarray[int64_t, ndim=2] nobs
@@ -851,27 +854,28 @@ def group_nth_%(name)s(ndarray[%(dest_type2)s, ndim=2] out,
851
854
852
855
N, K = (<object> values).shape
853
856
854
- for i in range(N):
855
- lab = labels[i]
856
- if lab < 0:
857
- continue
857
+ with nogil:
858
+ for i in range(N):
859
+ lab = labels[i]
860
+ if lab < 0:
861
+ continue
858
862
859
- counts[lab] += 1
860
- for j in range(K):
861
- val = values[i, j]
863
+ counts[lab] += 1
864
+ for j in range(K):
865
+ val = values[i, j]
862
866
863
- # not nan
864
- if val == val:
865
- nobs[lab, j] += 1
866
- if nobs[lab, j] == rank:
867
- resx[lab, j] = val
867
+ # not nan
868
+ if val == val:
869
+ nobs[lab, j] += 1
870
+ if nobs[lab, j] == rank:
871
+ resx[lab, j] = val
868
872
869
- for i in range(len(counts) ):
870
- for j in range(K):
871
- if nobs[i, j] == 0:
872
- out[i, j] = %(nan_val)s
873
- else:
874
- out[i, j] = resx[i, j]
873
+ for i in range(ncounts ):
874
+ for j in range(K):
875
+ if nobs[i, j] == 0:
876
+ out[i, j] = %(nan_val)s
877
+ else:
878
+ out[i, j] = resx[i, j]
875
879
"""
876
880
877
881
group_add_template = """@cython.wraparound(False)
@@ -1720,48 +1724,50 @@ def group_ohlc_%(name)s(ndarray[%(dest_type2)s, ndim=2] out,
1720
1724
raise NotImplementedError("Argument 'values' must have only "
1721
1725
"one dimension")
1722
1726
else:
1723
- for i in range(N):
1724
- while b < ngroups - 1 and i >= bins[b]:
1725
- if not got_first:
1726
- out[b, 0] = NAN
1727
- out[b, 1] = NAN
1728
- out[b, 2] = NAN
1729
- out[b, 3] = NAN
1730
- else:
1731
- out[b, 0] = vopen
1732
- out[b, 1] = vhigh
1733
- out[b, 2] = vlow
1734
- out[b, 3] = vclose
1735
- b += 1
1736
- got_first = 0
1737
1727
1738
- counts[b] += 1
1739
- val = values[i, 0]
1728
+ with nogil:
1729
+ for i in range(N):
1730
+ while b < ngroups - 1 and i >= bins[b]:
1731
+ if not got_first:
1732
+ out[b, 0] = NAN
1733
+ out[b, 1] = NAN
1734
+ out[b, 2] = NAN
1735
+ out[b, 3] = NAN
1736
+ else:
1737
+ out[b, 0] = vopen
1738
+ out[b, 1] = vhigh
1739
+ out[b, 2] = vlow
1740
+ out[b, 3] = vclose
1741
+ b += 1
1742
+ got_first = 0
1740
1743
1741
- # not nan
1742
- if val == val:
1743
- if not got_first:
1744
- got_first = 1
1745
- vopen = val
1746
- vlow = val
1747
- vhigh = val
1748
- else:
1749
- if val < vlow:
1744
+ counts[b] += 1
1745
+ val = values[i, 0]
1746
+
1747
+ # not nan
1748
+ if val == val:
1749
+ if not got_first:
1750
+ got_first = 1
1751
+ vopen = val
1750
1752
vlow = val
1751
- if val > vhigh:
1752
1753
vhigh = val
1753
- vclose = val
1754
-
1755
- if not got_first:
1756
- out[b, 0] = NAN
1757
- out[b, 1] = NAN
1758
- out[b, 2] = NAN
1759
- out[b, 3] = NAN
1760
- else:
1761
- out[b, 0] = vopen
1762
- out[b, 1] = vhigh
1763
- out[b, 2] = vlow
1764
- out[b, 3] = vclose
1754
+ else:
1755
+ if val < vlow:
1756
+ vlow = val
1757
+ if val > vhigh:
1758
+ vhigh = val
1759
+ vclose = val
1760
+
1761
+ if not got_first:
1762
+ out[b, 0] = NAN
1763
+ out[b, 1] = NAN
1764
+ out[b, 2] = NAN
1765
+ out[b, 3] = NAN
1766
+ else:
1767
+ out[b, 0] = vopen
1768
+ out[b, 1] = vhigh
1769
+ out[b, 2] = vlow
1770
+ out[b, 3] = vclose
1765
1771
"""
1766
1772
1767
1773
arrmap_template = """@cython.wraparound(False)
0 commit comments