astropy / astropy/astropy

LombScargle missing check for components of array-like objects with t.Time contents

Open
#18,212 4 comments 0 reactions 0 assignees View on GitHub
Bug timeseries
Dominant language
Python
Stars
5.3k
Forks
2.2k
Avg merge
1d 19h
Merged PRs (30d)
75

Description

### Description

When instanciating a LombScargle object, using a `np.array` of `t.Time` values for the t parameter, the checks for `isinstance(self.t, TimeDelta) (l.119)`, `isinstance(t, Time) (l.128)` return `False`, causing the constructor to assume the entries of `t` are unitless. This creates an issue when the frequency and powers are to be returned. `.autopower()` and `.power()` (the latter used with, or without units) raise the ValueError from l.143, because the validation fails. The LombScargle object itself can be created and will not raise an exception during initialisation.

This issue also happens with `pd.Series` where the contents of the series are `t.Time` objects, where it initially caused a bug for me.

This issue is reproducable in the current development version and was initially encountered in v6.1.

### Expected behavior

Naively, all inputs to t that fail the above mentioned checks for `t.Time` and `t.DeltaTime` are iterable because they are to be array-like (cf. l.40).

A simple check could be either
```python
if not False in [isinstance(t_val, t.Time) for t_val in self.t]:
...
```
(checking all values), or
```python
if isinstance(self.t[0], t.Time):
...
```
(just checking the leading value).

### How to Reproduce

Install python, astropy and its dependencies.
Run the following example code.
To raise the exceptions, instead of catching them in the except block, just remove the `try: except:` blocks.

```python
from astropy.timeseries import LombScargle
from astropy import time as t, units as u
import numpy as np

rand = np.random.default_rng(42)
t0 = t.Time(60000, format="mjd")

t_space = np.array(t0 + 100 * rand.random(100) * u.day)
y_space = np.array(rand.random(100) * u.mag + 15 * u.mag)

f_space_s = np.linspace(1, 100, 10**3) * 1/u.second
f_space_hz = np.linspace(1, 100, 10**3) * u.Hz
f_space_ul = np.linspace(1, 100, 10**3)

print(f"{'Intentionally causing Errrors':-^80}")
try:
print(f"Trying autopower()")
frequency, power = LombScargle(t_space, y_space).autopower()
except ValueError as ve:
print("ValueError message reads:\n", ve)
try:
print(f"Trying with {f_space_s.unit}")
power = LombScargle(t_space, y_space).power(f_space_s)
except ValueError as ve:
print("ValueError message reads:\n", ve)

try:
print(f"Trying with {f_space_hz.unit}")
power = LombScargle(t_space, y_space).power(f_space_hz)
except ValueError as ve:
print("ValueError message reads:\n", ve)

try:
print(f"Trying with {f_space_ul.unit}")
power = LombScargle(t_space, y_space).power(f_space_ul)
except ValueError as ve:
print("ValueError message reads:\n", ve)
except AttributeError as ae:
print("AttributeError message reads:\n", ae)

print(f"{'Checking the properties of our time input':-^80}")
print(f"Is t_space of instance t.Time? {isinstance(t_space, t.Time)}")
print(f"Are the contents of t_space of instance t.Time? {isinstance(t_space[0], t.Time)}")
```

This returns:
```text
-------------------------Intentionally causing Errrors--------------------------
Trying autopower()
ValueError message reads:
frequency have units while 1/t doesn't.
Trying with 1 / s
ValueError message reads:
frequency have units while 1/t doesn't.
Trying with Hz
ValueError message reads:
frequency have units while 1/t doesn't.
AttributeError message reads:
'numpy.ndarray' object has no attribute 'unit'
-------------------Checking the properties of our time input--------------------
Is t_space of instance t.Time? False
Are the contents of t_space of instance t.Time? True
```

### Versions

```python
platform
--------
platform.platform() = 'macOS-15.5-arm64-arm-64bit-Mach-O'
platform.version() = 'Darwin Kernel Version 24.5.0: Tue Apr 22 19:54:33 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T8122'
platform.python_version() = '3.13.3'

packages
--------
astropy 7.2.dev102+g33a13c0220
numpy 2.2.6
scipy 1.15.3
matplotlib 3.10.3
pandas 2.2.3
pyerfa 2.0.1.5
```

Contributor guide

Open the contributing guide

Research direction

Start at the LombScargle constructor, especially the checks around lines 119 and 128, then follow the validation near line 143. Reproduce the issue with the provided NumPy array and pandas Series examples and check autopower() and power() with unitful and unitless frequencies. Done means array-like inputs containing Time values are recognized and these calls no longer fail because t is treated as unitless.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, pandas, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.