Skip to content

[BLD] Fix remaining compile-time warnings #21940

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

Closed
wants to merge 10 commits into from
13 changes: 9 additions & 4 deletions pandas/_libs/groupby.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ from numpy cimport (ndarray,
int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t,
uint32_t, uint64_t, float32_t, float64_t)

cdef extern from "numpy/npy_math.h":
# Note: apparently npy_isnan has better windows-compat than
# the libc.math.isnan implementation
# See discussion: https://github.com/cython/cython/issues/550
bint npy_isnan(double x) nogil

from util cimport numeric, get_nat

Expand All @@ -35,7 +40,7 @@ cdef inline float64_t median_linear(float64_t* a, int n) nogil:

# count NAs
for i in range(n):
if a[i] != a[i]:
if npy_isnan(a[i]):
na_count += 1

if na_count:
Expand All @@ -46,7 +51,7 @@ cdef inline float64_t median_linear(float64_t* a, int n) nogil:

j = 0
for i in range(n):
if a[i] == a[i]:
if not npy_isnan(a[i]):
tmp[j] = a[i]
j += 1

Expand Down Expand Up @@ -160,7 +165,7 @@ def group_cumprod_float64(float64_t[:, :] out,
continue
for j in range(K):
val = values[i, j]
if val == val:
if not npy_isnan(val):
accum[lab, j] *= val
out[i, j] = accum[lab, j]
else:
Expand Down Expand Up @@ -199,7 +204,7 @@ def group_cumsum(numeric[:, :] out,
val = values[i, j]

if numeric == float32_t or numeric == float64_t:
if val == val:
if not npy_isnan(val):
accum[lab, j] += val
out[i, j] = accum[lab, j]
else:
Expand Down
28 changes: 18 additions & 10 deletions pandas/_libs/groupby_helper.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def group_add_{{name}}(ndarray[{{dest_type2}}, ndim=2] out,
val = values[i, j]

# not nan
if val == val:
if not npy_isnan(val):
nobs[lab, j] += 1
sumx[lab, j] += val

Expand Down Expand Up @@ -112,7 +112,7 @@ def group_prod_{{name}}(ndarray[{{dest_type2}}, ndim=2] out,
val = values[i, j]

# not nan
if val == val:
if not npy_isnan(val):
nobs[lab, j] += 1
prodx[lab, j] *= val

Expand Down Expand Up @@ -161,7 +161,7 @@ def group_var_{{name}}(ndarray[{{dest_type2}}, ndim=2] out,
val = values[i, j]

# not nan
if val == val:
if not npy_isnan(val):
nobs[lab, j] += 1
oldmean = mean[lab, j]
mean[lab, j] += (val - oldmean) / nobs[lab, j]
Expand Down Expand Up @@ -209,7 +209,7 @@ def group_mean_{{name}}(ndarray[{{dest_type2}}, ndim=2] out,
for j in range(K):
val = values[i, j]
# not nan
if val == val:
if not npy_isnan(val):
nobs[lab, j] += 1
sumx[lab, j] += val

Expand Down Expand Up @@ -260,10 +260,10 @@ def group_ohlc_{{name}}(ndarray[{{dest_type2}}, ndim=2] out,

counts[lab] += 1
val = values[i, 0]
if val != val:
if npy_isnan(val):
continue

if out[lab, 0] != out[lab, 0]:
if npy_isnan(out[lab, 0]):
out[lab, 0] = out[lab, 1] = out[lab, 2] = out[lab, 3] = val
else:
out[lab, 1] = max(out[lab, 1], val)
Expand Down Expand Up @@ -340,7 +340,11 @@ def group_last_{{name}}(ndarray[{{dest_type2}}, ndim=2] out,
val = values[i, j]

# not nan
{{if c_type.startswith('float')}}
if not npy_isnan(val) and val != {{nan_val}}:
{{else}}
if val == val and val != {{nan_val}}:
{{endif}}
nobs[lab, j] += 1
resx[lab, j] = val

Expand Down Expand Up @@ -396,7 +400,11 @@ def group_nth_{{name}}(ndarray[{{dest_type2}}, ndim=2] out,
val = values[i, j]

# not nan
{{if c_type.startswith('float')}}
if not npy_isnan(val) and val != {{nan_val}}:
{{else}}
if val == val and val != {{nan_val}}:
{{endif}}
nobs[lab, j] += 1
if nobs[lab, j] == rank:
resx[lab, j] = val
Expand Down Expand Up @@ -651,7 +659,7 @@ def group_max_{{name}}(ndarray[{{dest_type2}}, ndim=2] out,
{{if name == 'int64'}}
if val != {{nan_val}}:
{{else}}
if val == val and val != {{nan_val}}:
if not npy_isnan(val) and val != {{nan_val}}:
{{endif}}
nobs[lab, j] += 1
if val > maxx[lab, j]:
Expand Down Expand Up @@ -706,7 +714,7 @@ def group_min_{{name}}(ndarray[{{dest_type2}}, ndim=2] out,
{{if name == 'int64'}}
if val != {{nan_val}}:
{{else}}
if val == val and val != {{nan_val}}:
if not npy_isnan(val) and val != {{nan_val}}:
{{endif}}
nobs[lab, j] += 1
if val < minx[lab, j]:
Expand Down Expand Up @@ -754,7 +762,7 @@ def group_cummin_{{name}}(ndarray[{{dest_type2}}, ndim=2] out,
out[i, j] = {{nan_val}}
else:
{{else}}
if val == val:
if not npy_isnan(val):
{{endif}}
mval = accum[lab, j]
if val < mval:
Expand Down Expand Up @@ -795,7 +803,7 @@ def group_cummax_{{name}}(ndarray[{{dest_type2}}, ndim=2] out,
out[i, j] = {{nan_val}}
else:
{{else}}
if val == val:
if not npy_isnan(val):
{{endif}}
mval = accum[lab, j]
if val > mval:
Expand Down
1 change: 1 addition & 0 deletions pandas/_libs/hashtable.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cnp.import_array()

cdef extern from "numpy/npy_math.h":
double NAN "NPY_NAN"
bint npy_isnan(double x) nogil


from khash cimport (
Expand Down
50 changes: 27 additions & 23 deletions pandas/_libs/hashtable_class_helper.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dtypes = [('Float64', 'float64', 'float64_t'),

ctypedef struct {{name}}VectorData:
{{arg}} *data
size_t n, m
Py_ssize_t n, m

{{endif}}

Expand Down Expand Up @@ -147,7 +147,7 @@ cdef class StringVector:
cdef resize(self):
cdef:
char **orig_data
size_t i, m
Py_ssize_t i, m

m = self.data.m
self.data.m = max(self.data.m * 4, _INIT_VEC_CAP)
Expand All @@ -172,7 +172,7 @@ cdef class StringVector:
def to_array(self):
cdef:
ndarray ao
size_t n
Py_ssize_t n
object val

ao = np.empty(self.data.n, dtype=np.object)
Expand All @@ -198,7 +198,7 @@ cdef class ObjectVector:

cdef:
PyObject **data
size_t n, m
Py_ssize_t n, m
ndarray ao
bint external_view_exists

Expand Down Expand Up @@ -281,7 +281,7 @@ cdef class {{name}}HashTable(HashTable):
def sizeof(self, deep=False):
""" return the size of my table in bytes """
return self.table.n_buckets * (sizeof({{dtype}}_t) + # keys
sizeof(size_t) + # vals
sizeof(Py_ssize_t) + # vals
sizeof(uint32_t)) # flags

cpdef get_item(self, {{dtype}}_t val):
Expand Down Expand Up @@ -389,7 +389,11 @@ cdef class {{name}}HashTable(HashTable):
for i in range(n):
val = values[i]

{{if dtype == 'float64'}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe .startswith

if npy_isnan(val) or (use_na_value and val == na_value2):
{{else}}
if val != val or (use_na_value and val == na_value2):
{{endif}}
labels[i] = na_sentinel
continue

Expand Down Expand Up @@ -522,13 +526,13 @@ cdef class StringHashTable(HashTable):
def sizeof(self, deep=False):
""" return the size of my table in bytes """
return self.table.n_buckets * (sizeof(char *) + # keys
sizeof(size_t) + # vals
sizeof(Py_ssize_t) + # vals
sizeof(uint32_t)) # flags

cpdef get_item(self, object val):
cdef:
khiter_t k
char *v
const char *v
v = util.get_c_string(val)

k = kh_get_str(self.table, v)
Expand All @@ -541,7 +545,7 @@ cdef class StringHashTable(HashTable):
cdef:
khiter_t k
int ret = 0
char *v
const char *v

v = util.get_c_string(val)

Expand All @@ -560,10 +564,10 @@ cdef class StringHashTable(HashTable):
int64_t *resbuf = <int64_t*> labels.data
khiter_t k
kh_str_t *table = self.table
char *v
char **vecs
const char *v
const char **vecs

vecs = <char **> malloc(n * sizeof(char *))
vecs = <const char **> malloc(n * sizeof(char *))
for i in range(n):
val = values[i]
v = util.get_c_string(val)
Expand All @@ -589,10 +593,10 @@ cdef class StringHashTable(HashTable):
object val
ObjectVector uniques
khiter_t k
char *v
char **vecs
const char *v
const char **vecs

vecs = <char **> malloc(n * sizeof(char *))
vecs = <const char **> malloc(n * sizeof(char *))
uindexer = np.empty(n, dtype=np.int64)
for i in range(n):
val = values[i]
Expand Down Expand Up @@ -627,12 +631,12 @@ cdef class StringHashTable(HashTable):
Py_ssize_t i, n = len(values)
int ret = 0
object val
char *v
const char *v
khiter_t k
int64_t[:] locs = np.empty(n, dtype=np.int64)

# these by-definition *must* be strings
vecs = <char **> malloc(n * sizeof(char *))
vecs = <const char**> malloc(n * sizeof(char *))
for i in range(n):
val = values[i]

Expand Down Expand Up @@ -660,12 +664,12 @@ cdef class StringHashTable(HashTable):
Py_ssize_t i, n = len(values)
int ret = 0
object val
char *v
char **vecs
const char *v
const char**vecs
khiter_t k

# these by-definition *must* be strings
vecs = <char **> malloc(n * sizeof(char *))
vecs = <const char**> malloc(n * sizeof(char *))
for i in range(n):
val = values[i]

Expand Down Expand Up @@ -693,8 +697,8 @@ cdef class StringHashTable(HashTable):
Py_ssize_t idx, count = count_prior
int ret = 0
object val
char *v
char **vecs
const char *v
const char**vecs
khiter_t k
bint use_na_value

Expand All @@ -705,7 +709,7 @@ cdef class StringHashTable(HashTable):

# pre-filter out missing
# and assign pointers
vecs = <char **> malloc(n * sizeof(char *))
vecs = <const char**> malloc(n * sizeof(char *))
for i in range(n):
val = values[i]

Expand Down Expand Up @@ -769,7 +773,7 @@ cdef class PyObjectHashTable(HashTable):
def sizeof(self, deep=False):
""" return the size of my table in bytes """
return self.table.n_buckets * (sizeof(PyObject *) + # keys
sizeof(size_t) + # vals
sizeof(Py_ssize_t) + # vals
sizeof(uint32_t)) # flags

cpdef get_item(self, object val):
Expand Down
5 changes: 5 additions & 0 deletions pandas/_libs/hashtable_func_helper.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,12 @@ def ismember_{{dtype}}({{scalar}}[:] arr, {{scalar}}[:] values, bint hasnans=0):
if k != table.n_buckets:
result[i] = 1
else:
{{if dtype == 'float64'}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

result[i] = hasnans and npy_isnan(val)
{{else}}
result[i] = hasnans and val != val
{{endif}}

{{endif}}

kh_destroy_{{ttype}}(table)
Expand Down
9 changes: 9 additions & 0 deletions pandas/_libs/src/datetime/np_datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ This file is derived from NumPy 1.7. See NUMPY_LICENSE.txt
#define PyInt_AsLong PyLong_AsLong
#endif

// Silence "implicit declaration of function" warnings
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't these be in the .h?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that would make them public. This particular fix will be superseded by #21962 anyway.

int convert_datetimestruct_to_datetime(NPY_DATETIMEUNIT base,
const npy_datetimestruct *dts,
npy_datetime *out);
int convert_timedelta_to_timedeltastruct(NPY_DATETIMEUNIT base,
npy_timedelta td,
pandas_timedeltastruct *out);


const npy_datetimestruct _NS_MIN_DTS = {
1677, 9, 21, 0, 12, 43, 145225, 0, 0};
const npy_datetimestruct _NS_MAX_DTS = {
Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/src/parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ static int make_stream_space(parser_t *self, size_t nbytes) {
("\n\nmake_stream_space: nbytes = %zu. grow_buffer(self->stream...)\n",
nbytes))
self->stream = (char *)grow_buffer((void *)self->stream, self->stream_len,
(size_t*)&self->stream_cap, nbytes * 2,
(int64_t*)&self->stream_cap, nbytes * 2,
sizeof(char), &status);
TRACE(
("make_stream_space: self->stream=%p, self->stream_len = %zu, "
Expand All @@ -289,7 +289,7 @@ static int make_stream_space(parser_t *self, size_t nbytes) {
cap = self->words_cap;
self->words =
(char **)grow_buffer((void *)self->words, self->words_len,
(size_t*)&self->words_cap, nbytes,
(int64_t*)&self->words_cap, nbytes,
sizeof(char *), &status);
TRACE(
("make_stream_space: grow_buffer(self->self->words, %zu, %zu, %zu, "
Expand Down Expand Up @@ -320,7 +320,7 @@ static int make_stream_space(parser_t *self, size_t nbytes) {
cap = self->lines_cap;
self->line_start =
(int64_t *)grow_buffer((void *)self->line_start, self->lines + 1,
(size_t*)&self->lines_cap, nbytes,
(int64_t*)&self->lines_cap, nbytes,
sizeof(int64_t), &status);
TRACE((
"make_stream_space: grow_buffer(self->line_start, %zu, %zu, %zu, %d)\n",
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/src/util.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ cdef extern from "numpy_helper.h":
int assign_value_1d(ndarray, Py_ssize_t, object) except -1
cnp.int64_t get_nat()
object get_value_1d(ndarray, Py_ssize_t)
char *get_c_string(object) except NULL
const char *get_c_string(object) except NULL
object char_to_string(char*)

ctypedef fused numeric:
Expand Down
Loading