This question already has answers here:
Get date column from numpy loadtxt()
(2 answers)
Closed 4 years ago.
I have an array of dates in the format ('yyyy-mm-dd') and another array of integers numbers, each corresponding to a value in the date array. But, when I tried to plot the graph using:
matplotlib.pyplot.plot(dates, values, label='Price')
It gives the error:
ValueError: could not convert string to float: '2017-07-26'
How do I fix this error?
Your dates are strings, convert them to datetime objects first.
import datetime
x = [datetime.datetime.strptime(date, "%Y-%m-%d") for date in dates]
Related
This question already has answers here:
Replacing blank values (white space) with NaN in pandas
(13 answers)
Closed 1 year ago.
empty string like this isnull() not find empty string
conn = connect(host='localhost',port=3306,user='root',password='root',database='spiderdata',charset='utf8')
df = pd.read_sql('select * from beikedata_community1',con=conn)
df
df.subway.isnull()
**i want to use 'isnull()' find missing value, but it's not support empty string, what can i do? thanks very much!**
You can use print(df.replace(r' ', 'NaN')) .
This Replaces the empty cells with NaN.
This question already has answers here:
How to extract the n-th maximum/minimum value in a column of a DataFrame in pandas?
(3 answers)
Closed 3 years ago.
I have a data frame with a DateTime column, I can get minimum value by using
df['Date'].min()
How can I get the second, third... smallest values
Use nlargest or nsmallest
For second largest,
series.nlargest(2).iloc[-1]
Make sure your dates are in datetime first:
df['Sampled_Date'] = pd.to_datetime(df['Sampled_Date'])
Then drop the duplicates, take the nsmallest(2), and take the last value of that:
df['Sampled_Date'].drop_duplicates().nsmallest(2).iloc[-1]
This question already has answers here:
Add Leading Zeros to Strings in Pandas Dataframe
(7 answers)
Closed 3 years ago.
I have a data frame like this:
Date
9/02/2019
12/08/2019
8/06/2019
I want to add a 0 in infront of dates that are in single digit. I want it to be like this:
Date
09/02/2019
12/08/2019
08/06/2019
I am using this RegEx and the string manipulation. The string manipulation by itself works, but when I try to work it out with the RegEx, it doesn't yield anything.
if row['Date'] == r'[\d]{1}/[\d]{1,2}/[\d]{4}':
row['blah'] = '0' + row['Date']
print(row['blah'])
else:
pass
Use pd.to_datetime and convert it to string using Series.dt.strftime:
df['Date'] = pd.to_datetime(df['Date'], dayfirst=True)
df['Date'] = df['Date'].dt.strftime('%d/%m/%Y')
print(df)
Date
0 09/02/2019
1 12/08/2019
2 08/06/2019
This question already has answers here:
Converting Time Formats in Excel
(2 answers)
Closed 6 years ago.
May I know the way to convert "1 h 49 m 57 s" to 1:49:57 in Excel.
Thanks in advance
You can use this formula to replace letters to colons then convert text to value:
=VALUE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A5," h ",":")," m ",":")," s",""))
After inserting the formula set the cell's number format to "time".
This question already has answers here:
What are the "standard unambiguous date" formats for string-to-date conversion in R?
(8 answers)
Closed 9 years ago.
I need your help to figure out the following problem-
I am trying to convert a date column from string to actual date format. I have tried using as.Date
However, it is showing an error message:
Error in charToDate(x) :
character string is not in a standard unambiguous format
the date column I have in csv file is like this:
Date
03/17/2003
05/31/2003
09/06/2003
10/18/2003
07/15/2003
09/19/2003
The problem is some of the dates are in string and some are in actual date format. I have tried to format it from excel - didn't work
Tried to copy and paste it to notepad and then import it again - didn't work either.
You need to learn about the help system in R. One brief look at help(as.Date) may have told you about the format argument:
R> dt <- c("03/17/2003", "05/31/2003", "09/06/2003")
R> as.Date(dt, "%m/%d/%Y")
[1] "2003-03-17" "2003-05-31" "2003-09-06"
R>
Edit: These days we also have a helper package that does the format-finding for you:
> dt <- c("03/17/2003", "05/31/2003", "09/06/2003")
> anytime::anydate(dt)
[1] "2003-03-17" "2003-05-31" "2003-09-06"
>
This works for datetimes (using anytime()) and dates.