<p>Thanks to all for your help. Because of the planetmarker's comment I found out that it is also necessary to set the <code>time_bin_size</code> parameter. After upgrade to <code>lightkurve 2.0.6</code>, there is also parameter <code>bins</code>, which sets <code>time_bin_size</code> automatically.</p>
<pre><code>bin = fold.bin(bins=101)  # bins instead of n_bins
</code></pre>

CC BY-SA 4.0
2021-03-17T17:30:21.863Z
2021-03-17T17:30:21.863Z
Michal
<p>I am using <a href="https://docs.lightkurve.org/" rel="nofollow noreferrer">lightkurve</a> 2.0.2 library with Python 3.8.5 and astropy 4.2 for processing exoplanet transits. However when I want to bin light curve to fixed number of points, all values in <code>light_curve.flux</code> except first two are nan. What I am doing wrong?</p>
<p>The purpose of code bellow is to obtain transit of <strong>Kepler-10 a</strong> from light curve of <a href="https://en.wikipedia.org/wiki/Kepler-10" rel="nofollow noreferrer">Kepler-10</a>.</p>
<pre><code>import lightkurve as lk  # Import lighkurve package

tp = lk.search_targetpixelfile(&quot;Kepler-10&quot;, mission=&quot;Kepler&quot;, exptime=&quot;long&quot;, quarter=1).download()  # Download target pixel for Kepler-10
lc = tp.to_lightcurve().flatten().remove_outliers()  # Convert target pixel to light curve
fold = lc.fold(0.837)  # Fold light curve to period of Kepler-10 a (0.837 days)
bin = fold.bin(n_bins=101)  # Bin light curve, so it should contains only 101 points.

print(bin.flux)  # [0.99999749 0.99999977 nan nan nan nan nan nan nan ... nan nan nan nan]
</code></pre>
<p><code>lc</code> and <code>fold</code> are valid light curves without NaN values. E. g. <code>fold</code> looks like this:</p>
<p><a href="https://i.sstatic.net/dEHhe.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/dEHhe.png" alt="enter image description here" /></a></p>
<p>So if there is any problem, it should be on line with <code>fold.bin</code>. Also, this is not problem only for <code>Kepler-10</code>.</p>

CC BY-SA 4.0
2021-03-17T12:57:13.743Z
At any of those steps something might go wrong. You need to learn to debug your own code. For example, have you tried to bin data of which you know is not-NaN? Have you checked that the output of tp, lc, fold is reasonable?
AtmosphericPrisonEscape
CC BY-SA 4.0
2021-03-17T13:19:49.667Z
@AtmosphericPrisonEscape I checked everything and added more info to the question.
Michal
CC BY-SA 4.0
2021-03-17T13:40:54.477Z
Ok, then just to be 100% sure you could do np.count_nonzero(~np.isnan(fold)), as NaNs might not be plotted (although depending on whether you are using jupyer, there should be an alarm if there are NaNs in the array). I guess after that, the next step would be to try and bin some simple, self-created values, see if that works, compare to the real thing.
AtmosphericPrisonEscape
CC BY-SA 4.0
2021-03-17T14:04:02.823Z
@Michal I would appreciate if you could elaborate a bit on what you code is suppose to do (aka commenting each line). Also: Would your code run just like that in my Python console? I mean, is the data included in the packages? I would be more motivated to help you debugging if I would be sure that I do not have to invest loads of time to get it running. Otherwise: It sounds to a very interesting question for me :-)
B--rian
CC BY-SA 4.0
2021-03-17T16:22:59.070Z
@B--rian The code can run anywhere, just have Python and the lightkurve library. The data are downloaded from MAST archive using search_targetpixel function.
Michal
CC BY-SA 4.0
2021-03-17T16:34:46.840Z
try to remove all NaN values before you bin by calling LightCurve.remove_nans([column]). See https://docs.lightkurve.org/reference/lightcurve.html#other-utility-methods A plot will graciously ignore all NaN, thus the existence of a sensible plot does not say anything about an occasional NaN in the data.
planetmaker
CC BY-SA 4.0
2021-03-17T16:40:05.203Z
Also https://docs.lightkurve.org/reference/api/lightkurve.LightCurve.bin.html#lightkurve.LightCurve.bin suggests to me that you call that function with too few parameters. Or rather default time bin size is 0.5 days and your folded curve has a total length of 0.8 days, so only 2 bins can be filled with reasonable values when you request 101 bins. You assume an intelligence of the function it might not have
planetmaker
<exoplanet><python><planetary-transits>
Lightkurve nan values after bin light curve
Michal
# Lightkurve nan values after bin light curve

(Asked by: username_0 on 2021-03-17)

I am using lightkurve 2.0.2 library with Python 3.8.5 and astropy 4.2 for processing exoplanet transits. However when I want to bin light curve to fixed number of points, all values in light_curve.flux except first two are nan. What I am doing wrong?
The purpose of code bellow is to obtain transit of Kepler-10 a from light curve of Kepler-10.
import lightkurve as lk  # Import lighkurve package

tp = lk.search_targetpixelfile("Kepler-10", mission="Kepler", exptime="long", quarter=1).download()  # Download target pixel for Kepler-10
lc = tp.to_lightcurve().flatten().remove_outliers()  # Convert target pixel to light curve
fold = lc.fold(0.837)  # Fold light curve to period of Kepler-10 a (0.837 days)
bin = fold.bin(n_bins=101)  # Bin light curve, so it should contains only 101 points.

print(bin.flux)  # [0.99999749 0.99999977 nan nan nan nan nan nan nan ... nan nan nan nan]

lc and fold are valid light curves without NaN values. E. g. fold looks like this:

So if there is any problem, it should be on line with fold.bin. Also, this is not problem only for Kepler-10.

**Tags:** exoplanet, python, planetary-transits

> **Comments:**
> **username_1**: At any of those steps something might go wrong. You need to learn to debug your own code. For example, have you tried to bin data of which you know is not-NaN? Have you checked that the output of tp, lc, fold is reasonable?  
> **username_0**: @username_1 I checked everything and added more info to the question.  
> **username_1**: Ok, then just to be 100% sure you could do np.count_nonzero(~np.isnan(fold)), as NaNs might not be plotted (although depending on whether you are using jupyer, there should be an alarm if there are NaNs in the array). I guess after that, the next step would be to try and bin some simple, self-created values, see if that works, compare to the real thing.  
> **username_2**: @username_0 I would appreciate if you could elaborate a bit on what you code is suppose to do (aka commenting each line). Also: Would your code run just like that in my Python console? I mean, is the data included in the packages? I would be more motivated to help you debugging if I would be sure that I do not have to invest loads of time to get it running. Otherwise: It sounds to a very interesting question for me :-)  
> **username_0**: @username_2 The code can run anywhere, just have Python and the lightkurve library. The data are downloaded from MAST archive using search_targetpixel function.  
> **username_3**: try to remove all NaN values before you bin by calling LightCurve.remove_nans([column]). See https://docs.lightkurve.org/reference/lightcurve.html#other-utility-methods A plot will graciously ignore all NaN, thus the existence of a sensible plot does not say anything about an occasional NaN in the data.  
> **username_3**: Also https://docs.lightkurve.org/reference/api/lightkurve.LightCurve.bin.html#lightkurve.LightCurve.bin suggests to me that you call that function with too few parameters. Or rather default time bin size is 0.5 days and your folded curve has a total length of 0.8 days, so only 2 bins can be filled with reasonable values when you request 101 bins. You assume an intelligence of the function it might not have  

---

## 1 answers

### Answer by username_0 on 2021-03-17. Score: 3 | Accepted answer

Thanks to all for your help. Because of the planetmarker's comment I found out that it is also necessary to set the time_bin_size parameter. After upgrade to lightkurve 2.0.6, there is also parameter bins, which sets time_bin_size automatically.
bin = fold.bin(bins=101)  # bins instead of n_bins


---

